An iterator is an object that implements the iteration protocol, which consists of two methods: __iter__() and __next__().  The __iter__() method is called when an iterator object is initialized.The __next__() method is called to retrieve the next item from the iterator.

The StopIteration exception is raised when the __next__() method of an iterator object is called  but there are no items to iterate over. This exception is used to signal  the end of iteration.

ExampleEdit & Run
L = ["Pynerds", "Python", "Django"]

#Create a list iterator object
I = iter(L)

print(next(I))
print(next(I))
print(next(I))
print(next(I))
Output:
PynerdsPythonDjangoTraceback (most recent call last):  File "<string>", line 9, in <module>StopIteration[Finished in 0.010811490938067436s]

The builtin next() function is used to retrieve the next item from an iterator object. It actually calls the __next__() method of the iterator object. 

ExampleEdit & Run
#Define a generator function
def func():
   L = [0, 1, 2, 3]
   for i in L:
       yield i

gen = func()
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
print(next(gen))
Output:
0123Traceback (most recent call last):  File "<string>", line 12, in <module>StopIteration[Finished in 0.010028060991317034s]

Handling StopIteration exceptions

The most obvious way to handle a StopIteration exception is to use a for loop.  The for loop allows you to iterate over the sequence until a StopIteration exception is raised. The loop handles the exception automatically for you. 

ExampleEdit & Run
my_sequence = [1, 2, 3, 4, 5]

my_iter = iter(my_sequence)

for i in my_iter:
    print(i)
Output:
12345[Finished in 0.009998045861721039s]

You can also use the try-except blocks to handle the exception explicitly.

ExampleEdit & Run
my_list = ["Pynerds", "Python", "Django"]

#Create a list iterator object
my_iter = iter(my_list)

try:
   print(next(my_iter))
   print(next(my_iter))
   print(next(my_iter))
   print(next(my_iter))

except StopIteration:
    print("The iterator is exhausted.")
Output:
PynerdsPythonDjangoThe iterator is exhausted.[Finished in 0.009820041013881564s]

When using while loop:

ExampleEdit & Run
def func():
   L = [0, 1, 2, 3]
   for i in L:
       yield i

gen = func()

try:
    while True:
        print(next(gen))
except StopIteration:
    print("The iterator is exhausted.")
Output:
0123The iterator is exhausted.[Finished in 0.009363919030874968s]