Iterators & Generators

11. Iterators & Generators#

Iterators and generators are the backbone of Python’s memory-efficient data processing. They extend the functional style of the previous chapter: values are computed lazily, and small single-purpose stages compose into pipelines the way functions compose.

  • 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:

  1. Explain Python’s iterator protocol and use iter() and next() manually

  2. Use built-in lazy iterators such as enumerate(), zip(), map(), filter(), and reversed()

  3. Build custom iterator classes that implement the iterator protocol

  4. Write generator functions with yield and generator expressions, and delegate to another iterable with yield from

  5. Design multi-step data pipelines using chained generators and itertools tools such as islice and chain

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 __iter__ and __next__ methods that make an object work as an iterator.

4

iter

A built-in function that returns an iterator from an iterable.

5

next

A built-in function that asks an iterator for its next item.

6

StopIteration

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

enumerate

A lazy iterator that pairs each item with its index.

9

zip

A lazy iterator that pairs items from multiple iterables.

10

map

A lazy iterator that applies a function to each item.

11

filter

A lazy iterator that keeps items for which a function returns a truthy value.

12

reversed

A lazy iterator that returns the items of a sequence from last to first.

13

Custom iterator

A class that implements __iter__ and __next__ to define its own iteration logic.

14

Generator function

A function that uses yield to produce a sequence of values lazily.

15

Generator object

The iterator returned by calling a generator function; its body runs only as values are requested.

16

Reusable iterable

An object, such as a list or a class whose __iter__() is a generator, that returns a fresh iterator for every loop.

17

yield

A statement that produces a value from a generator and pauses its execution.

18

Generator expression

A compact expression that creates a lazy generator.

19

Short-circuit evaluation

Stopping as soon as the result is known, as any() and all() do with a generator expression.

20

Generator pipeline

A chain of generators in which each stage lazily consumes the output of the previous stage.

21

Consumer

The final step of a pipeline, such as sum(), list(), or a for loop, that pulls values through the stages.

22

Iterator exhaustion

The state of an iterator that has produced all its items and yields nothing on later passes.

23

Infinite sequence

A sequence that can keep producing values without a fixed end.

24

itertools

A standard-library module with tools for efficient iterator pipelines.

25

islice

An itertools function that takes selected positions from an iterator.

26

chain

An itertools function that connects multiple iterables into one stream.

27

yield from

Syntax for delegating part of a generator to another iterable (generator delegation).

28

Async generator

A function defined with async def that uses yield and is consumed with async for.

29

Async iteration

Iteration over values that may arrive asynchronously, commonly using async for.

Lecture Slides

Chapter 11 Slides