Generators in Python
A generator produces values one at a time with yield. def gen(): yield 1; yield 2. for x in gen(): prints 1 then 2. It does not build a full list in memory. Generator expression: (x * x for x in range(10)) — parentheses, not brackets. yield pauses. return ends.
Contrast with list comprehension: [...] builds everything now. Generator is lazy. That memory sentence is why interviews ask. next(g) also pulls one value.
Generators in Python — output — [1, 2, 3]. yield pauses and continues — not all at once like return.
yield vs return in one contrast.