Skip to main content

Iterators and Generators

Generator Functions

0:00
LearnStep 1/3

Generator Functions

In Python, a generator is a special type of iterator that allows you to iterate over a sequence of values without creating the entire sequence in memory at once. While a regular function uses return to send back a value and terminate, a generator function uses the yield keyword.

The yield Keyword

When a generator function calls yield, it pauses its execution and 'yields' a value to the caller. The function's state (including local variables) is saved, allowing it to resume exactly where it left off the next time it is called.

python

Memory-Efficient File Processing

Generators are essential for 'lazy evaluation.' Instead of reading a 10GB file into a list (which would crash most systems), you can yield one line at a time.

python

Generator Chains

You can chain generators to create clean, modular data processing pipelines. Each step in the pipeline only processes one item at a time as requested by the final consumer.

python

Using yield from

The yield from expression is a shorthand for delegating to a sub-generator. It replaces a for loop that simply yields values from another iterator.

python