Cyber Monday Sale: save up to $108/year Cyber Monday Sale: subscribe by Nov 28 to save up to $108/year Learn more

What can you do with generator expressions? PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
5 min. read 4 min. video Python 3.7—3.11

What can you do with generator expressions? In two words: lazy pre-processing.

We basically take the logic that's in a for loop and move some of it before the for loop. This allows us to chunk up our looping logic, which can sometimes make our code a little bit more readable.

Filtering elements using a for loop

We have a tab delimited file here (expenses.tsv) that represents prices of various items:

...

Sale Through November 28 💸

Subscribers get customized weekly recommendations for exercises and screencasts and access over 300 screencasts, exercises, and articles.

Save up to $108 per year if you subscribe by November 28.

Save on Python screencasts and exercises
A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.