Iterators and Iterables

Difference between Iterators and Iterables

Iterators, Iterables and for Loops

The Python forums and other question-and-answer websites like Quora and Stackoverflow are full of questions concerning 'iterators' and 'iterable'. Some want to know how they are defined and others want to know if there is an easy way to check, if an object is an iterator or an iterable. We will provide further down a function for this purpose.

We have seen that we can loop or iterate over various Python objects like lists, tuples and strings for example.

In [ ]:
for city in ["Berlin", "Vienna", "Zurich"]:
    print(city)
for city in ("Python", "Perl", "Ruby"):
    print(city)
for char in "Iteration is easy":
    print(char)

This form of looping can be seen as iteration. Iteration is not restricted to explicit for loops. If you call the function sum, - e.g. on a list of integer values, - you do iteration as well.

So what is the difference between an iterable and an iterator?

Difference Between Iterable and Iterator

In one perspective they are the same: You can iterate with a for loop over iterators and iterables. Every iterator is also an iterable, but not every iterable is an iterator. E.g. a list is iterable but a list is not an iterator! An iterator can be created from an iterable by using the function 'iter'. To make this possible the class of an object needs either a method '__iter__', which returns an iterator, or a '__getitem__' method with sequential indexes starting with 0.

Iterators are objects with a '__next__' method, which will be used when the function 'next' is called.

So what is going on behind the scenes, when a for loop is executed? The for statement calls iter() on the object ( which should be a so-called container object), which it is supposed to loop over. If this call is successful, the iter call will return return an iterator object that defines the method __next__() which accesses elements of the object one at a time. The __next__() method will raise a StopIteration exception, if there are no further elements available. The for loop whill terminate as soon as it catches a StopIteration exception. You can call the __next__() method using the next() built-in function. This is how it works:

cities = ["Berlin", "Vienna", "Zurich"]
iterator_obj = iter(cities)
print(iterator_obj)

print(next(iterator_obj))
print(next(iterator_obj))
print(next(iterator_obj))

If we called 'next(iterator_obj)' one more time, it would return 'StopIteration'

Check On being an Iterable or an Iterator

The following function 'iterable' will return True, if the object 'obj' is an iterable and False otherwise.

def iterable(obj):
     try:
         iter(obj)
         return True
     except TypeError:
         return False 
        
for element in [34, [4, 5], (4, 5), {"a":4}, "dfsdf", 4.5]:
    print(element, "iterable: ", iterable(element))
34 iterable:  False
[4, 5] iterable:  True
(4, 5) iterable:  True
{'a': 4} iterable:  True
dfsdf iterable:  True
4.5 iterable:  False

So we wrote a function which can decide, if a given object is an iterator. Shall we go on now and define another function, which can make such a decision for an 'iterator'. No! Even our function 'iterable' is not necessary, or we could say 'only good for didactical reasons'. The module collection.abc has the functionalities we need. We have to import the classes 'Iterator' and 'Iterable' from this module:

from collections.abc import Iterator, Iterable

cities_list = ["Berlin", "Frankfurt", "Geneva"]
cities_iterator = iter(cities_list)

print("Let's test 'cities_list':")
print("Iterator: ", isinstance(cities_list, Iterator))
print("Iterable: ", isinstance(cities_list, Iterable))

print("The same thing for  'cities_iterator':")
print("Iterator: ", isinstance(cities_iterator, Iterator))
print("Iterable: ", isinstance(cities_iterator, Iterable))
Let's test 'cities_list':
Iterator:  False
Iterable:  True
The same thing for  'cities_iterator':
Iterator:  True
Iterable:  True

Classes with Iterable Instances

We have described how an iterator works. So if you want to add an iterator behavior to your class, you have to add the __iter__ and the __next__ method to your class. The __iter__ method returns an iterator object. If the class contains a __next__, it is enough for the __iter__ method to return self, i.e. a reference to itself:

class Reverse:
    """
    Creates Iterators for looping over a sequence backwards.
    """
    
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]

lst = [34, 978, 42]
lst_backwards = Reverse(lst)
for el in lst_backwards:
    print(el)
42
978
34