Chapters
Python114 chapters

ReferenceException types

StopIteration

An iterator has nothing left. for loops catch this for you.

Signature

StopIteration

Returns

Raised by next() at the end.

Example

Python
it = iter([1])
print(next(it))
try:
    next(it)
except StopIteration:
    print("nothing left")

print(next(iter([]), "default instead"))

Output

1
nothing left
default instead

See also