A list comprehension builds the whole list in memory immediately, while a generator expression produces values one at a time, only when something asks for them. The syntax differs by the brackets: [x * x for x in data] is a list comprehension and (x * x for x in data) is a generator expression.
Choose a list comprehension when you need indexing, repeated passes or the length. Choose a generator expression when you only iterate once, especially over large or unbounded data.
Square brackets create a list. Parentheses create a generator object that has not computed anything yet.
The memory address in the second line varies. When a generator expression is the only argument of a function call, the extra parentheses can be dropped: sum(n * n for n in range(5)).
The list stores every element. The generator stores only its current state, so its size does not depend on how many values it will produce.
sys.getsizeof counts the list's pointer array but not the integer objects it refers to, so the real difference is even larger. Exact numbers depend on your Python version.
A generator computes each value when the loop requests it, and it can only be consumed once. After it is exhausted, it yields nothing.
Because evaluation is lazy, side effects and errors happen later than you may expect. In a generator expression, only the outermost iterable is evaluated immediately; the rest runs during iteration.
A generator supports only iteration. It has no len(), no indexing, no slicing, and you cannot go back.
If you need these features later, wrap the generator in list(...) once and reuse the result.
For small inputs, a list comprehension is often a little faster than the same work through a generator, because it avoids the overhead of resuming a generator for each item. For large inputs where you aggregate or stream, the generator wins on memory and can finish early when combined with short-circuiting functions.
Measure with timeit on your own data before optimising: the winner depends on input size and the work done per item.
| Need | Use |
|---|---|
| Iterate once over a large or streamed sequence | Generator expression |
Feed sum(), max(), any(), all(), "".join() | Generator expression |
| Reuse the results or loop more than once | List comprehension |
Need len(), indexing or slicing | List comprehension |
| Infinite or unbounded source | Generator expression (or a generator function) |
| Pass results to code that expects a list | List comprehension |
Once logic needs several statements, try blocks or state, write a generator function with yield instead of a dense expression.
It uses constant memory for the expression itself, yes. If you convert it with list() or keep references to every produced item, you lose the benefit.
Generators are single-use iterators. Once exhausted they stay empty. Recreate the expression or store the results in a list.
No. Parentheses create a generator expression. Use tuple(x for x in data) to build a tuple.
They are also eager: {x for x in data} builds a set and {k: v for k, v in pairs} builds a dict immediately. Only the parenthesised form is lazy.
List comprehensions are eager, reusable and support indexing. Generator expressions are lazy, single-use and memory efficient. Use brackets when you need the data as a list, and parentheses when you only need to stream through it once.