StopIteration
StopIteration is the Python exception raised when an iterator or generator has no further values to produce.
Python Workout: 50 ten-minute exercises
StopIteration is the Python exception raised by an exhausted iterator to signal that no further values are available.
- How it is used
- Signals iterator exhaustion so for loops and other consumers can terminate cleanly.
- How deep it goes
- Defines the protocol, traces custom implementation, contrasts helper iterators, and explains generator completion behavior.
- Examples
- Uses a traced LoudIterator, a helper iterator for repeated loops, generator functions, and a repeating Circle iterator.
- What you can do after
- Build it from scratch by implementing finite, reusable, or repeating Python iterators.
Read how Python Workout: 50 ten-minute exercises explains StopIteration →
Python Workout, Second Edition
StopIteration is the Python exception raised by an exhausted iterator to signal that no more values are available.
- How it is used
- Explains how Python for loops detect exhausted iterators and how custom iterators signal completion.
- How deep it goes
- Explains __iter__, __next__, stored iteration state, custom classes, and generator termination, but does not pursue advanced iterator designs fully.
- Examples
- Uses strings, lists, dictionaries, files, a traced LoudIterator, generator functions, and a chained generator.
- What you can do after
- Build iterable classes that return values from __next__ and terminate correctly with StopIteration.
Read how Python Workout, Second Edition explains StopIteration →
Also covered in
What to know first
- tupleA tuple is a finite, ordered collection of values grouped as a single composite value, often with elements of different types.
- comprehensionA comprehension is a language construct that creates a collection by iterating over one or more sources, optionally filtering elements and transforming them.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- lineA line is a contiguous sequence of text characters ending at a line boundary or the end of a text stream.
Related concepts
- iteratorpart of · An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- exceptionkind of · An exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.
- __next__produced by · __next__ is a Python special method that returns an iterator’s next item or raises StopIteration when no items remain.
- loopaddresses · A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- generatorused by · A generator is a language construct or computation that produces values incrementally on demand, typically suspending and resuming while preserving its state.
Questions about StopIteration
What is StopIteration?
StopIteration is the Python exception raised when an iterator or generator has no further values to produce.
Which Manning books explain StopIteration?
Python Workout: 50 ten-minute exercises and Python Workout, Second Edition.
What should I know before learning StopIteration?
Manning books that teach StopIteration usually introduce tuple, comprehension, loop and line first.
What is StopIteration a kind of?
StopIteration is a kind of exception. An exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.
What is StopIteration part of?
StopIteration is part of iterator. An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

