List, Set & Dictionary Comprehensions
8. Comprehensions, Iterator Protocol & Generator Architecture
8.1Declarative Comprehensions & C-Level Speed
Python comprehensions give us a compact, readable syntax to build lists, sets, or dictionaries in a single line of code!
Why are comprehensions faster? Under the hood, comprehensions execute loop iterations directly inside CPython's C implementation without repeatedly invoking Python bytecode overhead for .append() calls!
8.2The Iterator Protocol Under the Hood
Whenever you write a for element in collection: loop, Python uses the Iterator Protocol:
- It calls
iter(collection)to get an Iterator object. - It repeatedly calls
next(iterator)to fetch the next item one at a time. - When no items remain,
next()raises a special system signal calledStopIteration, which tells Python'sforloop to end gracefully.
8.3Generator Functions & Yield Mechanics
A Generator is a special type of function that returns data lazily—one piece at a time on demand—instead of building an entire list in memory all at once!
- The `yield` Keyword: When a generator function reaches a
yieldstatement, it pauses its execution, saves its variable state, and hands a single value to the caller. When requested again (next()), it resumes execution from the exact line where it paused! - Analogy Time: Think of a list like buying a 100-pack of bottled water and carrying the whole heavy box home at once. A generator is like turning on a kitchen tap: it dispenses one glass of water only when you turn the handle!
8.4Code Demo 1: List vs Generator RAM Footprint Benchmark
Compare memory usage between storing 1,000,000 numbers in a list versus generating them lazily:
8.5Code Demo 2: Lazy Fibonacci Stream Generator
Generate an infinite sequence of Fibonacci numbers using yield:
GOTCHA 1
Nesting comprehensions more than two levels deep. Overly complex comprehensions hurt readability and violate PEP 8. Use standard for loops when logic gets complex.
GOTCHA 2
Confusing condition placement: [x for x in data if condition] filters items, whereas [x if condition else default for x in data] applies ternary value transformation.
GOTCHA 3
Accidentally constructing a full list in memory when a generator expression (x for x in ...) was needed for streaming large datasets.
GOTCHA 4
Using set comprehensions when element ordering matters (sets do not preserve order!).
WHERE THIS SHOWS UP IN REAL CODE
Comprehensions provide concise, elegant syntax for transforming, filtering, and mapping data streams—such as cleaning database query records, extracting specific fields from JSON payloads, or building lookup maps.
TRY THIS YOURSELF
Modify Demo 2 to create a dict comprehension mapping u['name'] to u['role'] for all users.
Notice how cleanly transformation and filtering combine in a single line.
Demonstrate syntax, filtering clauses, dict comprehensions, and generator expressions.
Let's use a list comprehension to calculate squares of even numbers!
Steps to Complete:
- Given
numbers = range(1, 11). - Use list comprehension
[x*x for x in numbers if x % 2 == 0]to produce[4, 16, 36, 64, 100]. - Print
Even Squares: [4, 16, 36, 64, 100].
Expected Output:
Let's use a dictionary comprehension to filter and double values!
Steps to Complete:
- Given
payload = {'a': 5, 'b': 12, 'c': 3, 'd': 20}. - Use dict comprehension to keep keys where value
> 5and double their values. - Print
Filtered Doubled: {'b': 24, 'd': 40}.
Expected Output:
Let's use a set comprehension to normalize and alphabetize names!
Steps to Complete:
- Given
names = ['alice', 'bob', 'alice', 'charlie', 'bob']. - Use set comprehension to uppercase names, then sort into an alphabetical list.
- Print
Unique Names: ['ALICE', 'BOB', 'CHARLIE'].
Expected Output:
What is the correct syntax for a basic list comprehension with filtering?
What is the main advantage of a Generator Expression `(x for x in ...)` over a List Comprehension `[x for x in ...]`?
What does the comprehension `{k: v for k, v in data.items() if v > 0}` return?
Where should the condition be placed if you want to apply a ternary value transformation `(x if x > 0 else 0)` for ALL items?
Why should you avoid writing triple-nested comprehensions?
What type of object does `{x for x in [1, 2, 2, 3]}` construct?
What will `{n: n**2 for n in range(3)}` evaluate to?
# comments · comprehensions
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.