ALL MODULESmodule_12.py

List, Set & Dictionary Comprehensions

~45 minTheoryLive CodingChallengeQuiz
theory.md

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!

PYTHON

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:

  1. It calls iter(collection) to get an Iterator object.
  2. It repeatedly calls next(iterator) to fetch the next item one at a time.
  3. When no items remain, next() raises a special system signal called StopIteration, which tells Python's for loop 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 yield statement, 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:

PYTHON

8.5Code Demo 2: Lazy Fibonacci Stream Generator

Generate an infinite sequence of Fibonacci numbers using yield:

PYTHON
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.

live_coding — Log Event Sanitizer & Dict Map Comprehensions

Notice how cleanly transformation and filtering combine in a single line.

Python not loaded yet.
demo — Demonstrate syntax, filtering clauses, dict comprehensions, and generator expressions.

Demonstrate syntax, filtering clauses, dict comprehensions, and generator expressions.

challenge_1

Let's use a list comprehension to calculate squares of even numbers!

Steps to Complete:

  1. Given numbers = range(1, 11).
  2. Use list comprehension [x*x for x in numbers if x % 2 == 0] to produce [4, 16, 36, 64, 100].
  3. Print Even Squares: [4, 16, 36, 64, 100].

Expected Output:

TEXT
Python not loaded yet.
challenge_2

Let's use a dictionary comprehension to filter and double values!

Steps to Complete:

  1. Given payload = {'a': 5, 'b': 12, 'c': 3, 'd': 20}.
  2. Use dict comprehension to keep keys where value > 5 and double their values.
  3. Print Filtered Doubled: {'b': 24, 'd': 40}.

Expected Output:

TEXT
Python not loaded yet.
challenge_3

Let's use a set comprehension to normalize and alphabetize names!

Steps to Complete:

  1. Given names = ['alice', 'bob', 'alice', 'charlie', 'bob'].
  2. Use set comprehension to uppercase names, then sort into an alphabetical list.
  3. Print Unique Names: ['ALICE', 'BOB', 'CHARLIE'].

Expected Output:

TEXT
Python not loaded yet.
quiz — 7 questions
30s question timers are paused. Start individually or all together.
# question 1

What is the correct syntax for a basic list comprehension with filtering?

# question 2

What is the main advantage of a Generator Expression `(x for x in ...)` over a List Comprehension `[x for x in ...]`?

# question 3

What does the comprehension `{k: v for k, v in data.items() if v > 0}` return?

# question 4

Where should the condition be placed if you want to apply a ternary value transformation `(x if x > 0 else 0)` for ALL items?

# question 5

Why should you avoid writing triple-nested comprehensions?

# question 6

What type of object does `{x for x in [1, 2, 2, 3]}` construct?

# question 7

What will `{n: n**2 for n in range(3)}` evaluate to?

discussion

# 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.