Understanding and resolving StopIteration in Python
The StopIteration error happens when you try to get the next item from an iterator that has no more items left.
Common causes of this error...
# Code that causes the error numbers = iter([1, 2, 3]) print(next(numbers)) print(next(numbers)) print(next(numbers)) print(next(numbers))
# Fixed code
numbers = iter([1, 2, 3])
try:
while True:
print(next(numbers))
except StopIteration:
pass