1.2.36 · D1Introduction to Programming (Python)

Foundations — Generator expressions — memory efficiency

1,671 words8 min readBack to topic

Before you can trust the parent note's claim that a generator uses memory while a list uses , you must own every symbol and word it leans on. We build them from nothing, in the order they depend on each other. Nothing is used before it is defined.


1. What is an item? (the atom of everything)

Picture a single sandwich on a plate. That one sandwich is an item. When we later say "one item at a time", we mean exactly one plate in your hands.

Figure — Generator expressions — memory efficiency

Why the topic needs it: the whole memory argument is "how many items exist at the same moment". So "item" is the unit we will count.


2. What is a collection / sequence?

Picture a whole stack of plates on a table. Each plate is an item (section 1); the stack is the collection. The key question that this entire topic answers is:

Must all the plates sit on the table at once, or can we serve them one plate at a time?

A list is the "all plates on the table" answer. A generator is the "one plate at a time" answer.


3. The letter — "how many"

Why a letter instead of a number? Because we want a statement that is true for every size at once. Writing lets us say "no matter how big the stack gets…". Picture the stack of plates growing taller and taller — is its height.

We will meet again inside , so lock it in now: = count of items.


4. Memory — the desk, not the kitchen

Picture two desks side by side.

  • Left desk (a list): all plates stacked on it simultaneously. Big stack → big desk needed.
  • Right desk (a generator): exactly one plate at any instant, plus a tiny sticky-note that says "how to make the next plate".
Figure — Generator expressions — memory efficiency

Why the topic needs it: "memory efficiency" is literally how much desk space you need. The sticky-note is the crucial trick — read on.

See Memory management in Python for how Python actually allocates this desk space.


5. The recipe (state) — the sticky-note

For the sticky-note holds just two things: "the last I used" and "the rule: square it, then step up by one". That note stays the same size whether the sequence is 5 items or 5 billion — it never lists the items, only how to get the next one.

This is the heart of Lazy evaluation: compute a value only when it is actually asked for, never in advance.


6. Iterate / on demand / yield one at a time

Picture pressing a "NEXT" button. Each press: the current plate leaves your hand, the recipe makes the following plate, and it lands in your hand. You are never holding two.

Figure — Generator expressions — memory efficiency

The mechanism behind the NEXT button is the iterator protocol — see Iterators and the iterator protocol. The def/yield sibling that produces values the same way is covered in Generator functions and yield.


7. Exhausted — the button stops working

Picture the printer after the final page: press NEXT and it just clicks, empty. This is why the parent warns you can't loop a generator twice. The plates you ate are gone; to eat again you must write a new recipe.


8. Big-O: and — measuring how the desk grows

Now the symbols and . They describe how the desk space changes as grows, ignoring fixed clutter.

Picture two lines on a graph as increases:

Figure — Generator expressions — memory efficiency

Why "1" for a generator? Because "one plate + one sticky-note" is a fixed amount that does not depend on . In Big-O we call any fixed amount — literally "grows like the constant 1". Deeper treatment lives in Big-O space complexity.


9. The syntax symbols: () vs [] and the pieces inside

That single-character swap is the difference between the two desks in section 4.


Prerequisite map

Item = one value

Collection = many items in order

n = how many items

Memory = the desk right now

Recipe / state = tiny sticky note

Iterate = press NEXT one at a time

On demand = made only when asked

Exhausted = NEXT stops working

Big-O = how desk grows with n

Parens vs brackets

Generator expression memory efficiency


Equipment checklist

You are ready for the parent note if you can answer each of these out loud:

An item is
a single value — the smallest thing we count (one sandwich / one plate).
The symbol means
the number of items in a collection.
Memory here is best pictured as
the desk surface — only what is held right now counts.
The generator's recipe (state) stores
where we are + the rule for the next item — a fixed-size sticky-note, not the items.
To iterate means
to ask for items one after another, next only after finishing the current.
On demand / lazy means
a value is computed only at the moment it is asked for, never in advance.
A generator becomes exhausted when
you press NEXT past the last item; further presses yield nothing.
describes memory that
grows in proportion to the number of items — double , double the space.
describes memory that
stays constant regardless of .
The one-character difference between a list comprehension and a generator expression is
() parentheses instead of [] square brackets.

Now proceed to the parent topic.