StopIteration in Python — What It Means and How to Fix It

What Python's StopIteration exception means and how to resolve it.

Published October 28, 2026

StopIteration is raised internally by an iterator to signal there are no more items; usually handled automatically by for loops.

Key points

  • Catch it specifically with "except StopIteration:" when you want to handle this exact case rather than a bare except

Example

next(iter([]))  # StopIteration
More Python articles