How to Use a Generator Expression in Python
A list comprehension builds a whole list in memory. A generator expression looks almost the same but produces its values one at a time, only when something asks for them. It's the natural fit for feeding values straight into sum(), max() or "".join(). This guide shows how it works and its one surprise, with real output from Python 3.15.2.
Syntax
(<expression> for <item> in <iterable>)
(<expression> for <item> in <iterable> if <condition>)
| Part | What it means |
|---|---|
( ... ) | Round brackets make a generator; square brackets would make a list |
<expression> | The value produced for each item, such as price * 2 |
for <item> in <iterable> | Where the items come from |
if <condition> | Optional: keep only items that pass |
When the generator is the only argument of a call, the call's own brackets are enough: sum(price * 2 for price in prices).
Python's language reference: "The syntax for generator expressions is the same as for list comprehensions, except that they are enclosed in parentheses instead of brackets." At run time it "evaluates to a generator iterator which yields the same values as the corresponding list comprehension".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
prices = [12.5, 30.0, 7.25]
print(sum(price * 2 for price in prices))
squares = (n * n for n in range(4))
print(list(squares))
print(list(squares))

Step 2: Run It
uv run python generator.py
99.5
[0, 1, 4, 9]
[]
![The output in a terminal: 99.5, then [0, 1, 4, 9], then an empty list.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-generator-02-output.webp)
sum() took each doubled price as it was produced: 25.0 + 60.0 + 14.5 = 99.5, with no list built in between. The second list(squares) is empty: a generator hands out its values once, and the first list() took them all.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Reading a generator twice | The second read gets nothing: [] | Make a new generator, or use a list if you need the values again |
Asking for its length with len() | TypeError: object of type 'generator' has no len() | Use a list, or count with sum(1 for _ in ...) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Printing it shows <generator object <genexpr> at 0x...> | A generator only describes the values; it doesn't hold them | Wrap it in list() to see the values |
FAQ
When should I use a list comprehension instead?
When you need the values more than once, need len(), or need to index into them. Otherwise a generator does the same work without the list.
Is a generator faster?
It saves memory, because the values never sit in a list together. For small inputs like this one, the difference doesn't matter.
Conclusion
sum(price * 2 for price in prices) added the doubled prices to 99.5 without building a list, and the second list(squares) came back empty because a generator gives its values only once.
Additional Resources
- Generator expressions — the syntax and what it evaluates to.
