Understanding Iterables vs Iterators in Python

Though often confused with one another, Iterables and Iterators are two distinct concepts. This article will explain the difference between the two, and how they are used.

Understanding Iterables vs Iterators in Python
Image by geralt on Pixabay


Iterables and Iterators are often confused for one another, however, they are two distinct concepts. This article will explain the difference between the two, and how they are used. Let’s first briefly look at what Iteration is.

In layman’s terms, Iteration means ‘repeating steps’. In programming terms, Iterations is the repetition of a statement/block of code a specific number of times, producing an output one after another. Iterations can be executed by using for loops for example. 


Iterables in Python

Iterables are objects which can be looped/iterated over by using a for loop. Iterables contain data or have value, where executing a for loop will perform an iteration, producing outputs one after another. 

An iterable implements the __iter__() method and returns an iterator object. However, if the __iter__() method is not defined, Python will use __getitem__() instead. 

Examples of Iterables are lists, dictionaries, strings, tuples, and more. As long as you can loop over it, it is an Iterable. 

To figure out whether an object is iterable or not, you will have to check if it supports __iter__. In order to do this, we use the dir() function, which returns all properties and methods of the specified object, excluding values. 

Example Code:

class Person:
 name = "Nisha"
 age = 25
 country = "England"



['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__', 'age', 'country', 'name']


Iterators in Python

An Iterator is also an object which uses the __iter__() and __next__() methods, this is called the Iterator protocol. It is an iterable object with a state, meaning it remembers what stage it is at during iteration. 

Iterators return values, one element at a time. As the next value of an Iterable object is being returned, the state of the iterator is updated and knows how to get to the next value using the __next__() method. Iterators can only move forward, they cannot go back or reset themselves. 

Iterators also raise a StopIteration exception when there are no more elements or the object is exhausted. 

Example Code:

Here I have created an iterator called ‘numbers’. I have put in a line of code to check its type. We are expecting the iteration to output all the numbers. However, I have asked it to print the 6th output, although there are only 5 values in the iterator. Let’s see what happens

numbers = iter([2, 4, 6, 8, 10])


# The next() function will raise StopIteration as it is exhausted


We can see that the type is a ‘list_iterator’. The output stopped at 10 and raised a StopIteration as there were no more elements in the numbers list. 

<class 'list_iterator'>

StopIteration                             Traceback (most recent call last)
<ipython-input-15-0cb721c5f355> in <module>()
     12 # The bext() function will raise StopIteration as it is exhausted
---> 13 print(next(numbers))


Limitations of Iterators:

  1. Iterators only move in a forward direction, they do not go backwards or reset.
  2. Iterators cannot be copied as it is a one-way object that can only move forward.
  3. Due to its one-way direction, there is no way to retrieve the previous element. 


Similarities and Differences Between Iterables and Iterators


Iterable Iterator
Iterated by using: for loop  for loop 
Methods used:  __iter__()     __iter__() and __next__()


An Iterator is an Iterable, as it also implements the __iter__() method. 

Remember: Every Iterator is an Iterable, however not every Iterable is an Iterator. 

Example of dir() of an Iterable:

numbers = [2, 4, 6, 8, 10]



In this output, I have highlighted __iter__, showing that it is a method of an Iterable.

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', 
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', 
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', 
'__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Example of dir() of an Iterator:

In this example, we are iterating the numbers list. 

numbers = [2, 4, 6, 8, 10]
numbers2 = iter(numbers)



In this output, I have highlighted __iter__ and __next__, showing that it is a method of an Iterator.

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', 
'__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', 
'__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', 
'__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', 

I hope this short blog has given you a better understanding of the differences between an Iterable and an Iterator.

Nisha Arya is a Data Scientist and freelance Technical writer. She is particularly interested in providing Data Science career advice or tutorials and theory based knowledge around Data Science. She also wishes to explore the different ways Artificial Intelligence is/can benefit the longevity of human life. A keen learner, seeking to broaden her tech knowledge and writing skills, whilst helping guide others.