⚙️
Intermediate
List · Dict · Set · Generator expressions · yield
Week 2 — Comprehensions & Generators
Write concise, readable data transformations with list, dict, and set comprehensions. Then step into generators and yield for memory-efficient iteration.
comprehensiongeneratoryieldlazy evaluation
Duration
⏱ 2.5 hours
Level
📊 Intermediate
Prerequisite
🎯 Basic Week 6–7
OUTCOME
Replace verbose for-loops with comprehensions and write a generator pipeline
What you'll learn
- 1Write list, dict, and set comprehensions with filtering
- 2Use nested comprehensions for 2D data
- 3Explain the difference between a list comprehension and a generator expression
- 4Write generator functions with yield
- 5Chain generators for memory-efficient pipelines
1. List Comprehensions
python
# Basic: [expression for item in iterable if condition]
squares = [x**2 for x in range(1, 11)]
evens = [x for x in range(20) if x % 2 == 0]
matrix = [[row*col for col in range(1,4)] for row in range(1,4)]
print(squares)
print(evens)
print(matrix)💡
If the resulting list is only iterated once, prefer a generator expression () instead of [] to save memory.
2. Dict & Set Comprehensions
python
words = ["apple", "banana", "cherry"]
lengths = {w: len(w) for w in words}
print(lengths) # {'apple': 5, 'banana': 6, 'cherry': 6}
unique_lengths = {len(w) for w in words}
print(unique_lengths) # {5, 6}3. Generator Expressions & yield
python
# Generator expression (lazy — doesn't compute all at once)
gen = (x**2 for x in range(1_000_000))
print(next(gen)) # 0
print(next(gen)) # 1
# Generator function
def countdown(n):
while n > 0:
yield n
n -= 1
for v in countdown(5):
print(v, end=' ') # 5 4 3 2 14. Common Mistakes
- Deeply nested comprehensions hurt readability — if you need three levels, use a regular loop.
- Generators are exhausted after one pass. If you need to iterate twice, convert to list first.
- Using a walrus operator (:=) in comprehensions is valid but can make code hard to read.
💻 Examples
Run these examples and check the output yourself.
01_comprehensions.py— List / dict / set comprehensions
CODE
numbers = list(range(1, 21))
squares = [n**2 for n in numbers]
even_sq = [n**2 for n in numbers if n % 2 == 0]
sq_dict = {n: n**2 for n in range(1, 6)}
uniq = {n % 5 for n in range(20)}
print(squares[:5])
print(even_sq)
print(sq_dict)
print(uniq)
▶ Output
[1, 4, 9, 16, 25]
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400]
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{0, 1, 2, 3, 4}02_flatten.py— Flatten a nested list with a nested comprehension
CODE
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat = [item for sublist in nested for item in sublist]
print(flat)
▶ Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]03_generator_pipeline.py— Lazy pipeline for large data
CODE
def read_lines(n):
for i in range(n):
yield f"log line {i}: value={i*10}"
def parse_value(lines):
for line in lines:
yield int(line.split('=')[1])
def filter_large(values, threshold):
for v in values:
if v > threshold:
yield v
result = filter_large(parse_value(read_lines(20)), 100)
print(list(result))
▶ Output
[110, 120, 130, 140, 150, 160, 170, 180, 190]📝 Exercises
Try them yourself first, then open the solution to compare.
Exercise 1
Matrix Transpose
Goal: Transpose a 3×3 matrix using a list comprehension.
Requirements
- Input: [[1,2,3],[4,5,6],[7,8,9]]
- Output: [[1,4,7],[2,5,8],[3,6,9]]
- Use a single comprehension expression
▶Toggle solution
SOLUTION
matrix = [[1,2,3],[4,5,6],[7,8,9]]
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
for row in transposed:
print(row)
▶ Output
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]Exercise 2
Infinite Fibonacci Generator
Goal: Write fib_gen() that yields Fibonacci numbers indefinitely.
Requirements
- Yields 0, 1, 1, 2, 3, 5, 8, ... infinitely
- Use islice from itertools to take first N values
▶Toggle solution
SOLUTION
from itertools import islice
def fib_gen():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
print(list(islice(fib_gen(), 10)))
▶ Output
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]Example code / lecture materials
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗