Part 2 · Section 04
Iterables and Iterators
Sequences can be iterated because you can ask them for item 0, item 1, item 2 and so on. But many collections, like sets, dictionaries, files and infinite streams of values, have no meaningful index. Python solves this with a more general idea: an object you can repeatedly ask for the next item. Every for loop, comprehension, sum(), sorted(), zip() and unpacking assignment is built on it.
The most important idea of this section, and of Part 2, is the difference between an iterable (something you can loop over, again and again) and an iterator (the single-use object that does the looping and remembers where it is). Once that distinction clicks, a whole family of puzzling bugs, like “why is my second loop empty?”, become obvious.
In this section you’ll learn:
- why indexing isn’t enough, and how
__next__andStopIterationgive a more general iteration model - the iterator protocol (
__iter__returningselfplus__next__) and what “exhausted” means - the iterable protocol:
__iter__returning a fresh iterator, and why separating the two fixes re-iteration - consuming iterators by hand with
next(), for example to read a file’s header rows - infinite and lazy iterables that compute their values only when asked
- which built-ins (
range,zip,enumerate, files, dict views,map…) are iterables and which are iterators - the two forms of
iter(), includingiter(callable, sentinel), and the sequence-protocol fallback - delegating iteration to a contained collection, and supporting
reversed()with__reversed__ - the classic trap of passing an iterator to a function that needs to loop twice
Lectures
- 01Iterating Collections✓
- 02Iterators✓
- 03Iterators and Iterables✓
- 04Example 1 - Consuming Iterators Manually✓
- 05Example 2 - Cyclic Iterators✓
- 06Lazy Iterables✓
- 07Python's Built-In Iterables and Iterators✓
- 08Sorting Iterables✓
- 09The iter() Function✓
- 10Iterating Callables✓
- 11Delegating Iterators✓
- 12Reversed Iteration✓
- 13Caveat Using Iterators as Function Arguments✓