Iterators & Generators
Generator Expressions
Memory-efficient lazy evaluation using generator expressions — the parenthesized form of list comprehensions that produces values on demand.
Interview: Performance optimization question — know when to use generator expressions vs list comprehensions.
Generator expressions look like list comprehensions but use parentheses instead of brackets. They produce values lazily — one at a time — making them ideal for large datasets where you don't need all values in memory simultaneously.
Syntax and Comparison
(x**2 for x in range(n))— generator expression (lazy)[x**2 for x in range(n)]— list comprehension (eager, builds entire list)- Generator expressions use constant memory regardless of the data size
- List comprehensions are faster for small datasets (no generator overhead)
When to Use Each
- Generator expression: When iterating once, passing to sum/max/min/any/all, or processing large data
- List comprehension: When you need indexing, len(), multiple iterations, or the full dataset
- Functions like
sum(),max(),any()accept generators directly — no need for a list
Interview Insight
Know when to use sum(x**2 for x in range(n)) (generator) vs sum([x**2 for x in range(n)]) (list). The generator version uses O(1) memory; the list version uses O(n).
Use Cases
Aggregation — sum(), max(), min() over large datasets without building a list
File processing — transforming lines from a file one at a time
Data pipelines — chaining filter/transform operations lazily
Searching — any()/all() for short-circuit evaluation on large data
Memory-constrained environments — processing data that exceeds available RAM
Common Mistakes
Using list comprehension when you only iterate once — wastes memory
Trying to index or get len() of a generator expression — not supported
Forgetting generator is single-use — calling sum() then max() gives 0 for max
Adding unnecessary brackets: sum([x for x in ...]) instead of sum(x for x in ...)
Not considering that generator overhead makes lists faster for small datasets