Generator Expression

say it: “JEN-uh-ray-ter ek-SPRESH-un” /ˈdʒɛnəreɪtər ɪkˈsprɛʃən/

Like a list comprehension in round brackets, producing values one at a time.

What is a generator expression?

A generator expression looks like a list comprehension with round brackets, but it produces values one at a time instead of building a whole list.

prices = [12.5, 8.0, 30.0]
total = sum(price * 1.07 for price in prices)   # no list is built
print(round(total, 2))  # 54.04

Level: Freelancer · More Python terms at this level

Updated