Skip to main content

How to Use a Generator Expression in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
(<expression> for <item> in <iterable>)
(<expression> for <item> in <iterable> if <condition>)
PartWhat 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​

generator.py
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))

The generator.py script in a macOS-style code window: the sum of each price doubled, then a generator of squares turned into a list twice.

Step 2: Run It​

Terminal
uv run python generator.py
Output
99.5
[0, 1, 4, 9]
[]

The output in a terminal: 99.5, then [0, 1, 4, 9], then an empty list.

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​

MistakeWhat happensDo this instead
Reading a generator twiceThe 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​

SymptomLikely causeFix
Printing it shows <generator object <genexpr> at 0x...>A generator only describes the values; it doesn't hold themWrap 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​