Python Basics - 09. Comprehensions, Iterators, and Generators¶
This notebook introduces Pythonic ways to transform data efficiently. You will learn list/dict/set comprehensions, how iteration works under the hood, and how generators help you process large data lazily.
Download Notebook¶
1. List Comprehensions¶
A list comprehension is a concise way to create a list from another iterable.
numbers = [1, 2, 3, 4, 5, 6]
squares = [n * n for n in numbers]
even_squares = [n * n for n in numbers if n % 2 == 0]
print(f"Numbers: {numbers}")
print(f"Squares: {squares}")
print(f"Even squares: {even_squares}")
Numbers: [1, 2, 3, 4, 5, 6]
Squares: [1, 4, 9, 16, 25, 36]
Even squares: [4, 16, 36]
2. Dict and Set Comprehensions¶
The same pattern can build dictionaries and sets in one readable expression.
words = ["python", "is", "clean", "and", "powerful"]
length_map = {word: len(word) for word in words}
first_letters = {word[0] for word in words}
print(f"Length map: {length_map}")
print(f"First letters set: {first_letters}")
Length map: {'python': 6, 'is': 2, 'clean': 5, 'and': 3, 'powerful': 8}
First letters set: {'i', 'a', 'c', 'p'}
3. Iterators and the Iterator Protocol¶
Any iterable can return an iterator with iter(). The iterator yields values one by one with next() until StopIteration.
colors = ["red", "green", "blue"]
it = iter(colors)
print(next(it))
print(next(it))
print(next(it))
# next(it) # Uncomment to raise StopIteration
red
green
blue
4. Generator Expressions¶
A generator expression looks like a list comprehension but uses parentheses. It computes values lazily, so memory usage is smaller.
big_range = range(1, 1_000_001)
sum_of_squares = sum(n * n for n in big_range)
print(f"Sum of squares from 1..1,000,000: {sum_of_squares}")
Sum of squares from 1..1,000,000: 333333833333500000
5. Generator Functions with yield¶
Use yield to produce a sequence over time. The function pauses between values and resumes where it stopped.
def fibonacci(limit):
a, b = 0, 1
while a <= limit:
yield a
a, b = b, a + b
print(list(fibonacci(50)))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]