11. Iterators & Generators#
Iterators and generators are the backbone of Python’s memory-efficient data processing.
The iterator protocol:
__iter__and__next__Built-in lazy iterators
Custom iterator classes
Generator functions and generator expressions
Lazy pipelines for streaming data
Chapter Video
This overview introduces generator functions and lazy value production with yield.
Python Tutorial: Generators - How to use them and the benefits you receive (11:14) by Corey Schafer
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Explain Python’s iterator protocol and use
iter()andnext()manuallyUse built-in lazy iterators such as
enumerate(),zip(),map(),filter(), andreversed()Build custom iterator classes that implement the iterator protocol
Write generator functions with
yieldDesign multi-step data pipelines using chained generators
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Iterable |
An object that can provide its items one at a time. |
2 |
Iterator |
An object that returns one item at a time and remembers its position. |
3 |
Iterator protocol |
The |
4 |
|
A built-in function that returns an iterator from an iterable. |
5 |
|
A built-in function that asks an iterator for its next item. |
6 |
|
The exception that signals an iterator has no more items. |
7 |
Lazy iterator |
An iterator that produces values on demand instead of building a full list first. |
8 |
|
A lazy iterator that pairs each item with its index. |
9 |
|
A lazy iterator that pairs items from multiple iterables. |
10 |
|
A lazy iterator that applies a function to each item. |
11 |
|
A lazy iterator that keeps items for which a function returns true. |
12 |
Generator function |
A function that uses |
13 |
|
A statement that produces a value from a generator and pauses its execution. |
14 |
Generator expression |
A compact expression that creates a lazy generator. |
15 |
Infinite sequence |
A sequence that can keep producing values without a fixed end. |
16 |
|
A standard-library module with tools for efficient iterator pipelines. |
17 |
|
An |
18 |
|
An |
19 |
|
Syntax for delegating part of a generator to another iterable. |
20 |
Async iteration |
Iteration over values that may arrive asynchronously, commonly using |
21 |
Generator pipeline |
A chain of generators in which each stage lazily consumes the output of the previous stage. |
22 |
Iterator exhaustion |
The state of an iterator that has produced all its items and yields nothing on later passes. |