Iterators, Iteration and Iterables
Iterator is an object with state so that it remembers where it is during iteration and iterator also know how to get the next value. Iterator get the next value with a dunder or magic method called next( ).
I think you did not get what exactly iterator is. So we will explore this concept more with some simple and understandable examples.
- Types of iterators
- How to check
- Protocols
- How to use Iterator?
- Stop Iteration Exception
- How to handle Stop Iteration Exception
- Conclusion
- Example
In Python objects can be of two types iterable and non-iterable.
- Iterable: If the object is capable of returning it’s member value one by one then it is called iterable object.
- Non-Iterable: If the object is not capable then it is non-iterable object.
In more simple and plain words iterable objects have collection of data and if collection of data is there then each data in collection will be accessed one by one and this is called iterable and the whole process is called iteration.
And,
For non-iterable objects the data act as a single entity and you can not access a single entity in steps. It should be accessed in one go. We will take some simple examples of iterable and non-iterable objects.
For example. I am taking a simple example of what is iteration means with the help of for loop.
Here I used a collection (list) to access one item at a time. So I can say list1 is iterable and varibale a is accessing list1 items one by one.
List, tuple, sets, dictionaries and strings are example of iterable objects.
This is the example of non-iterable object. When I apply for loop on ‘number’, it gives me error that “TypeError: ‘int’ object is not iterable” because ‘number’ is not a collection of data or sequence. ‘number’ is a single entity data and that is 12 here. I can not access 12 as 1 and 2. It must be accessed as 12 in one go.
How to check that if my object is an iterable or not?
We will use dir( ) method of python to check whether the object is iterable or not. If the object has dunder or magical method __ iter__ than it is an iterable otherwise it is not.
list1 has the magic method __iter__, So, it is iterable object. But if I do same for number then the magic method is not there and this proves that number is not a iterable object.
Protocols
Important:
The working of for loop in background is it is calling __iter__ method on object list1 and returning an iterator that we can loop over on the list1 items, So this is the reason for list1 has to be iterable.
Python has a concept of iterator. Iterator object is a dedicated topic in python and it comes in intermediate or advance level of topics. To implement iterator explicitly there are some protocols. Those protocols are iter( ) and next( ).
iter( ): To get an iterator we need to initialise iter( ) method.
next( ): To perform iteration over iterable object next( ) method initialised.
Important:
So if you look at the list1 objects methods next() is not there. This means our list1 object does not have a state to remember and it does not know how to get next value, therefore, it is not an itreator.
How to use Iterator?
Object to be a iterator it must run next( ) dunder method. If I run dunder next() method on list1 object then in background it is trying to run dunder next() method and we already seen that list1 does not have that one because it is not an itertor.
__iter__() is used to create the iterator over the iterable object.
We can easily see that after using dunder method it became iterator. Look at the below code when I used next( ) dunder method it gives me first element of list1.
To get successive elements of list1 I have to use next( ) method again and again.
Important: Dunder method iter( ) and next( ) both are present so it proves iter1 is an iterator.
Stop Iteration Exception
When a iterator executes all elements after that if we run that one more time than it will throw exception Stop Iteration.
When it throws exception it means iterator has been exhausted and it has no value now.
In simple words, If all the values from an iterator have been returned, a subsequent next()
call raises a StopIteration
exception.
How to handle Stop Iteration Exception
When we run a for loop then for loop knows how to handle this Stop Iteration exception.
In the background a for loop is getting iterator for the original object that is list1 and then it is getting the next values until it hits stop iteration exception.
One more similar solution like for loop is:
Conclusion
Iterators can only go forward and there is no going backward or nothing like making a copy. You can only go forward by calling next( ) dunder method.
If you need to start from beginning than you have to create a new iterator.
Example
- Build an iterator that returns multiple of 21.
2. Design a range function using iterator concept.
Thats all for Iterators. You can practice to gain more expertise on this concept.
If you like this article and find out useful then follow and share for others as well. At the time of writing this article, I have 11 followers. Show some love.