Skip to Content
Course content

173: Generator Expressions Revisited

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, isn't this just a list comprehension with different brackets?

On the surface, yes. If you look at [x**2 for x in range(10)] and (x**2 for x in range(10)), they look nearly identical. But the difference is fundamental: one creates the entire list in memory immediately, and the other creates a generator object that knows how to produce the next value only when you ask for it.

I usually tell people to think about it in terms of "now" vs "later." A list comprehension is "do it all now." A generator expression is "do it as we go." This is a lifesaver when you're dealing with massive datasets. For example, if you're processing a log file with ten million lines, you absolutely do not want to load a list of ten million strings into your RAM just to check if some of them contain the word "ERROR".

# This might crash your computer if the range is too large
squares_list = [x**2 for x in range(100000000)] 

# This happens instantly because it hasn't actually calculated anything yet
squares_gen = (x**2 for x in range(100000000)) 

Can I just drop the extra parentheses when passing these into functions?

Yes, and you should. This is one of those little Python syntactic sugars that makes the code look much cleaner. If a generator expression is the only argument being passed to a function, you don't need the double parentheses.

I've seen a lot of beginners write sum((x for x in range(10))). While that works, it's redundant. You can just write sum(x for x in range(10)). Python is smart enough to realize that the expression inside the function call is a generator.

# The "clunky" way
total = sum((i for i in range(1000)))

# The "Pythonic" way
total = sum(i for i in range(1000)) 

Why does my generator seem to "disappear" after I use it once?

This is the "gotcha" that trips everyone up the first time. Generators are iterators, and iterators are exhaustible. Once you've looped through a generator expression, it's spent. It doesn't reset. If you try to iterate over it a second time, it'll just give you an empty sequence.

I've spent an hour debugging a project before because I passed a generator to a validation function, and then tried to pass that same generator to a database save function. The validation function "consumed" the generator, leaving the save function with nothing to process. If you need the data more than once, you're better off converting it to a list or just recreating the generator.

nums = (x for x in range(3))

print(list(nums)) # Output: [0, 1, 2]
print(list(nums)) # Output: [] - It's empty now! 



📋 Practical Task

Calculating Average Peak Temperature from a Sensor Stream

Imagine you are working with a simulated stream of 1,000,000 temperature readings (represented as a range of floats). You need to calculate the average of only the "peak" temperatures—defined as any reading above 80.0 degrees.

To keep memory usage low, you must avoid creating any intermediate lists. Your task is to:

  • Create a generator expression that filters the raw data for values > 80.0.
  • Use that generator to calculate the sum of the peaks.
  • Use a second generator (or recreate the first) to count how many peaks there were.
  • Calculate the final average.

Constraint: Do not use list() or append() anywhere in your solution.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.