DEV Community

Cover image for Beginners guide to Iterator, Iteration and Iterable in Python
Shiva Gaire
Shiva Gaire

Posted on • Originally published at dev.shivagaire.com.np

Beginners guide to Iterator, Iteration and Iterable in Python

Introduction

In programming iterable, iterator and iteration are pretty familiar words and one can have much confusion explaining and understanding these pretty common topics. Let's demystify them one by one.

Iterable

The natural language definition of Iterable is capable of being repeated or iterated.
However, the pythonic definition of iterator is an object that implements these two magic methods: __iter__ and __getitem__.

The __iter__ method of iterable returns an iterator, which we will see next and similarly the __getitem__ method takes an argument or key item from its sequences starting index zero and until the index is no longer valid and python raises an exception called IndexError. The __getitem__ method is used mostly for index-based lookup over the collections.

In python default types: list & strings have__iter__ & __getitem__ implemented and hence we can refer them as an iterable.

For a more concise visual representation let's assume a single row bookshelf where there are 7 different books.

image.png

On this bookshelf, we can go through each book one by one, also we can change order of the books, can swap the places, hence we can consider this bookshelf as an object. But as a reader, I can read only one book at a time.
So, it can be clearly seen this bookshelf can be considered iterable from what we have discussed to this point.

Iterator

In programming, an iterator is an object that let us travel through the Iterable objects. This iterator object remembers where it is during the process of travelling.

In python, an iterator is an object with a __next__ method implemented in it. The
__next__ method returns the next value while traversing(travelling through) the iterable object sequence starting from index zero of the iterable. After each call, the __next__ method updates its state to point to the next object in the iterable sequence. And when the travelling process is completed, it raises the StopError exception.

For a visual representation, let's assume you have a toy laser light that you can use to point to the book sequentially to read through them one by one, during this process to take out the first book you first point your toy laser light to the first book in the bookshelf, you take out that book and while doing so the laser focus points to next book in the sequence and so on until going through them one by one completes.

image.png

>>> bookshelf = ['The Diary of a young girl', 'Crime and punishment', 'Romeo and Juliet', ...] #7 books
>>> flashlight  = iter(bookshelf)
>>> next(flashlight)
The Diary of a young girl
>>> next(flashlight)
Crime and punishment
Enter fullscreen mode Exit fullscreen mode

In python, the for loop, map and list comprehension automatically call this next method.

Iteration

The Merriam Webster dictionary defines iteration as:

  • the repetition of a sequence of computer instructions a specified number of times or until a condition is metβ€” compare RECURSION
  • one execution of a sequence of operations or instructions in an iteration.

From the above bookshelf analogy, we can say the process of repeating through book one by one can be referred to as an iteration.

In programming, while doing iteration the same block of code is repeated every time. We use loops to achieve iteration that basically executes instruction or sequence of instruction repeatedly.

Pythonic Conclusion

Iterable: list, strings, and any objects that have implemented __getitem__ and __iter__ methods.

Iterator: Use of iter() function on an iterable gives iterator object, Iterator has next method in it. This iterator object remember where it is during the iteration

Iteration: The use of for loop while loop or even list comprehension.
Please leave an example in the comment box :D

Reference

  1. Iterator
  2. Iterator Types
  3. Iterator, iterators and Iterations

Finally, If you learned something from this article, please share it with your friends.

You may also follow me on LinkedIn and Twitter .

Top comments (0)