2. Iterators and Iterables
By Bernd Klein. Last modified: 01 Feb 2022.
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 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:
for city in ["Berlin", "Vienna", "Zurich"]:
print(city)
for language in ("Python", "Perl", "Ruby"):
print(city)
for char in "Iteration is easy":
print(char)
OUTPUT:
Berlin Vienna Zurich Python Perl Ruby I t e r a t i o n i s e a s y
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?
On one hand, 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), over which it is supposed to loop . If this call is successful, the iter call will 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 will 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))
OUTPUT:
<list_iterator object at 0x0000016FBDAEEC88> Berlin Vienna Zurich
If we called 'next(iterator_obj)' one more time, it would return 'StopIteration'
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))
OUTPUT:
34 iterable: False [4, 5] iterable: True (4, 5) iterable: True {'a': 4} iterable: True dfsdf iterable: True 4.5 iterable: False
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)
OUTPUT:
42 978 34
Live Python training
Enjoying this page? We offer live Python training courses covering the content of this site.
Upcoming online Courses