---
title: Python Iterators
---
Python supports a concept of iteration over containers. This is implemented using two distinct methods; these are used to allow user-defined classes to support iteration.
Python Docs - Iterator Types
Iteration is the process of programatically repeating a step a given number of times.  A programmer can make use of iteration to perform the same operation on every item in a collection of data, for example printing out every item in a list.
*   Objects can implement a `__iter__()` method that returns an iterator object to support iteration.
*   Iterator objects must implement:
    *   `__iter__()`: returns the iterator object.
    *   `__next__()`: returns the next object of the container.
    iterator_object = 'abc'.__iter__()
    print(iterator_object)
    print(id(iterator_object))
    print(id(iterator_object.__iter__())) # Returns the iterator itself.
    print(iterator_object.__next__())     # Returns 1st object and advances iterator.
    print(iterator_object.__next__())     # Returns 2nd object and advances iterator.
    print(iterator_object.__next__())     # Returns 3rd object and advances iterator.
    print(iterator_object.__next__())     # Raises StopIteration Exception.
Output :
    
    4343305888
    4343305888
    a
    b
    c
    ---------------------------------------------------------------------------
    StopIteration                             Traceback (most recent call last)
     in ()
          6 print(iterator_object.__next__())     # Returns 2nd object and advances iterator.
          7 print(iterator_object.__next__())     # Returns 3rd object and advances iterator.
    ----> 8 print(iterator_object.__next__())     # Raises StopIteration Exception.
    StopIteration: