Beginners guide to Iterator, Iterable and Iteration in Python
Understand Iterator, Iteration and Iterable in python
3 min read
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.
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__ 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:
__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.
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.
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.
'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 punishmentbookshelf = [
In python, the
map and list comprehension automatically call this
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.
Iterable: list, strings, and any objects that have implemented
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
while loop or even
Please leave an example in the comment box :D
Finally, If you learned something from this article, please share it with your friends.
Did you find this article valuable?
Support Shiva Gaire by becoming a sponsor. Any amount is appreciated!