1.2.36 · D3Introduction to Programming (Python)

Worked examples — Generator expressions — memory efficiency

2,613 words12 min readBack to topic

You already know the one-line idea from the parent note: a generator expression produces items one at a time, on demand. This page is a drill. We take that idea and hammer it against every kind of situation it can meet — normal cases, empty cases, infinite cases, the "consumed twice" trap, a real-world file problem, and an exam-style twist.

Before we begin, one promise: every symbol and every function that appears (sum, next, any, zip, itertools.islice, memory in bytes) will be explained the first time it shows up. If you have never typed Python in your life, start at line one and you will still follow.


The scenario matrix

Think of a generator expression as a machine with an input (some iterable), a rule (the expr), an optional filter (if cond), and a consumer (whoever pulls the values out). Each of these four parts can be normal, empty, degenerate, or pushed to a limit. The table below lists every case-class this topic can throw at you. Every cell gets a worked example below.

# Case class What is special about it Example
A Normal, fully consumed ordinary rule, ordinary consumer Ex 1
B Filter removes some if cond drops elements Ex 2
C Filter removes ALL (degenerate) generator yields nothing Ex 3
D Empty input (degenerate) iterable has zero items Ex 3
E Partial consumption pull only a few with next Ex 4
F Double consumption (the trap) exhausted after one pass Ex 5
G Infinite / limiting input never ends; must slice Ex 6
H Real-world word problem huge file, memory matters Ex 7
I Exam twist nested / lazy-capture gotcha Ex 8

Two supporting pictures first, then the examples.

Figure — Generator expressions — memory efficiency

The figure above is the mental model for memory (case classes A–H all live inside it). The list builds every box up front; the generator holds exactly one box and remembers the recipe. Look at the flat green line — that is , the meaning of "memory does not grow with ". ( and are explained in Big-O space complexity; here just read them as "constant" vs "grows in proportion to ".)

Figure — Generator expressions — memory efficiency

The second figure is the mental model for consumption: a generator has a pointer that only ever moves forward. That single arrow explains cases E, F and G at once — you can stop early (E), you can run out (F), and you can walk forever (G) — but you can never rewind.


The worked examples



Active Recall


Connections

  • Generator expressions — memory efficiency — the parent idea this page drills
  • Lazy evaluation — "computed on demand" powers Examples 4, 6, 8
  • Iterators and the iterator protocolnext() and the forward-only pointer
  • Generator functions and yield — the def/yield cousin, same laziness
  • List comprehensions — the eager alternative used in Examples 2, 8
  • Big-O space complexity — the vs shown in the first figure
  • Memory management in Python — why the huge-file case (Ex 7) matters

Generator expression

if cond runs first

expr transforms survivors

Fully consumed A B

Empty or all filtered C D

Pulled with next E

Second pass empty F

Infinite source needs islice G

Huge file any short circuit H

Value read at call time I