ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Python Tutorial• 7 min read

Generators & Memory-Efficient Iterators

The yield statement, infinite sequences, and generator expressions

Generators produce values on-the-fly one at a time using the yield keyword, allowing you to process multi-gigabyte datasets without exhausting system RAM.

How yield Preserves Function State

Unlike return which terminates a function, yield pauses the function, emits a value to the caller, and remembers its exact instruction pointer and local variables.

When next() is called, the function resumes immediately after the yield statement.

Generator expressions: (x ** 2 for x in range(1_000_000)) consumes 120 bytes of RAM, compared to [x ** 2 for ...] which consumes ~80MB!

Streaming Large Data with a Generator
def stream_large_dataset(max_records):
    """Streams data lazily without keeping everything in memory."""
    current = 1
    while current <= max_records:
        yield f"Record #{current}"
        current += 1

# Consuming generator lazily
gen = stream_large_dataset(1_000_000)
print(next(gen)) # Record #1
print(next(gen)) # Record #2
# Memory footprint remains ~120 bytes!
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which keyword turns a standard Python function into a generator that produces values lazily upon request?