1.2.36 · HinglishIntroduction to Programming (Python)

Generator expressions — memory efficiency

1,357 words6 min readRead in English

1.2.36 · Coding › Introduction to Programming (Python)


WHAT hai ek generator expression?

list_comp = [x*x for x in range(1_000_000)]   # builds 1,000,000 ints NOW
gen_expr  = (x*x for x in range(1_000_000))   # builds NOTHING yet
  • list_comp ek real list hai jisme ek million numbers stored hain.
  • gen_expr ek chhota sa generator object hai jo yaad rakhta hai kaise unhe produce karna hai.

WHY save hoti hai memory isse?

Toh memory cost yeh hai:

Object Memory used ke saath badhti hai?
items ki List saare items store karta hai Haan
Generator 1 item + recipe store karta hai Nahi

HOW karte hain actually use?

Aap ise iterate karke consume karte hain — for, sum(), next(), any(), etc. Har ek values ko ek baar consume karta hai.

# next() pulls ONE value
g = (x*x for x in range(5))
next(g)   # 0
next(g)   # 1   <- state advanced, 0 is gone
 
# A function that takes an iterable consumes the whole generator
total = sum(x*x for x in range(1_000_000))   # parentheses optional as sole arg
Figure — Generator expressions — memory efficiency

Worked Examples


Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tum 100 sandwiches khaana chahte ho. List tarika: pehle saari 100 banao, table par pile kar do (bada mess, bahut jagah). Generator tarika: kitchen next sandwich tabhi banata hai jab tum pichli kha lo. Aapke paath kabhi ek se zyada nahi hoti — toh almost koi table space nahi chahiye, chahe 100 hon ya million sandwiches. Ek baat: jab sab kha lo, toh gone — aap same wali dobara nahi kha sakte.


Active Recall

What punctuation distinguishes a generator expression from a list comprehension?
Parentheses () instead of square brackets [].
What is the memory complexity of a generator expression vs a list comprehension over n items?
Generator is O(1) (one item at a time); list is O(n) (all items stored).
Why does (x*x for x in range(n)) use almost no memory?
It stores only the recipe/state and yields each square on demand, never building the full sequence.
What happens when you iterate a generator a second time?
Nothing is produced — it is exhausted after one full pass.
When summing 1,000,000 squares, why prefer sum(x*x for x in range(10**6)) over a list comprehension?
The generator avoids allocating a million-element list; peak memory stays tiny.
Can you index a generator like g[0]?
No — generators are not subscriptable; you must use next() or iterate.
If a generator expression is the sole argument to a function, what can you drop?
The extra parentheses, e.g. sum(x*x for x in data).

Connections

  • List comprehensions — same syntax, eager evaluation
  • Iterators and the iterator protocol__iter__ / __next__ generators ko underpin karte hain
  • Generator functions and yield — generator expressions ka def/yield cousin
  • Lazy evaluation — "compute when needed" ka general principle
  • Big-O space complexity — kyun O(1) vs O(n) matter karta hai
  • Memory management in Python — lists ki heap allocation

Concept Map

same syntax but parens

stores all items

returns

produces

leads to

contrasts with

used via

advances state

fix by

Generator expression

List comprehension

Lazy iterator

Yields one item at a time

O of 1 memory

O of n memory

Consumed by for sum next

Exhausted after one pass

Rebuild or use list