A list comprehension builds the whole list in memory immediately, using square brackets. A generator expression, written with parentheses, produces its items one at a time, only when something asks for the next value. Use a generator expression when you iterate once or the data is large, and a list comprehension when you need the full list, indexing or repeated passes.
The syntax is almost identical, but memory use, laziness and reuse are very different.
Printing the generator shows only the generator object, because nothing has been computed yet. Converting it with list() runs it to the end.
| Feature | List comprehension | Generator expression |
|---|---|---|
| Brackets | [ ... ] | ( ... ) |
| Evaluation | Eager, all at once | Lazy, one item per request |
| Memory | Proportional to the number of items | Small and constant |
Indexing and len() | Supported | Not supported |
| Reuse | Iterate as often as you like | One pass, then exhausted |
| Result type | list | generator |
A list stores every element, so a million items need memory for a million references and their values. A generator stores only its current state.
The exact numbers vary by Python version and platform, but the list is thousands of times larger. sys.getsizeof measures only the container, not the integers it refers to, so the real gap for the list is even bigger.
A generator runs its loop only as values are requested, and it cannot be rewound.
Once exhausted, a generator yields nothing more. If you need the values twice, build a list, or create the generator again.
When a generator expression is the only argument of a call, the extra parentheses can be dropped. This is the most common and most efficient use.
sum, min, max, any, all and str.join accept any iterable, so no intermediate list is built. any and all also stop as soon as the answer is known, so a generator stops early too.
timeit on your own data rather than assuming. The difference depends on the size and the work done per item.Generators chain naturally into a pipeline that never holds the whole file in memory.
The file object is itself a lazy iterator of lines, so only one line is held at a time. The same task written with [line for line in f] would load the entire file first.
len(), indexing or slicing, or will pass it to code that expects a list.yield instead of a dense expression.Not always. For small data a list comprehension can be a little faster, while a generator saves memory and time to first result on large data. Benchmark your case.
No. A generator has no len() because it does not know how many items remain. Convert it to a list if you need the count, or count while iterating.
Generators are single-use. After the first full iteration they are exhausted, so recreate the expression or store the values in a list.
No. Parentheses create a generator expression. Use tuple(x for x in iterable) to build a tuple.
List comprehensions are eager and give you a reusable list. Generator expressions are lazy, memory-friendly and single-use. Default to a generator when you only iterate once or the data is large, and use a list when you need random access or several passes.