What is Python Iterator (Syntax & Example) – Create your own Iterator
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this Python Iterator Tutorial, we will learn what is Python iterator.
We will also discuss how to create our own __iter__() and __next__() methods, building a python iterator, for loop in python iterator, infinite python iterator, and benefits of an iterator in python with an example.
In our article on python in-built functions, one function we saw was iter(). Let us generate an iterator in python, which we traversed using the next() function.
S, let’s start Python Iterator Tutorial.
What are Python3 Iterators?
An iterator in Python programming language is an object which you can iterate upon. That is, it returns one object at a time.
Python Iterator, implicitly implemented in constructs like for-loops, comprehensions, and python generators.
The iter() and next() functions collectively form the iterator protocol.
If we can get iterable from an object in python, it is iterable. Examples include python lists, python tuples, and python strings.
How to Create Python Iterator?
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
To build a python3 iterator, we use the iter() and next() functions. Let’s begin with iter() to create an iterator.
First, we create a list of events to store even numbers from 2 to 10.
>>> evens=[2,4,6,8,10]
Then, we apply the iter() function to this Python list to create a Python iterator object. We store it in the variable evenIterator.
>>> evenIterator=iter(evens) >>> evenIterator
Output
Remember, it does not have to be a list you create an iterator on.
>>> iter((1,3,2))
Output
Now, to access the first element, we apply the function next() on the Python iterator object.
>>> next(evenIterator)
Output
We do the same for the next element(s) as well.
>>> next(evenIterator)
Output
>>> next(evenIterator)
Output
>>> next(evenIterator)
Output
>>> next(evenIterator)
Output
We have reached the end of the list. When we call it once more, we raise a StopIteration error (exception). The interpreter internally catches it.
>>> next(evenIterator)
Output
Traceback (most recent call last):File “<pyshell#442>”, line 1, in <module>next(evenIterator)StopIteration
- __next__()
You can also traverse the Python iterator using the __next__() method.
>>> nums=[1,2,3] >>> numIter=iter(nums) >>> numIter.__next__()
Output
>>> next(numIter)
Output
>>> numIter.__next__()
Output
>>> numIter.__next__()
Output
File “<pyshell#448>”, line 1, in <module>numIter.__next__()StopIteration
We can see this with the dir() function we saw in in-built functions in Python.
>>> dir(numIter)
Output
For-loop in Python Iterator
You can also use a for loop in python to iterate on an iterable like a python list or a python tuple.
>>> for i in 'Python': Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(i)
Output
y
t
h
o
n
But how is this actually implemented? Let’s take a look.
>>> iter_obj=iter('Python') >>> while True: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â try: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â i=next(iter_obj) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â print(i) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â except StopIteration: Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break
Output
y
t
h
o
n
So, this is how the above for loop is actually implemented.
How to Create Your Own Python Iterator?
Now you know how to use an iterator with the iter() and next() functions. But we won’t stop here. We will now start from scratch.
We implement the following class to create an iterator in Python for squares of numbers from 1 to max.
>>> class PowTwo:                def __init__(self,max=0):                                self.max=max                def __iter__(self):                                self.n=0                                return self                def __next__(self):                                if self.n<=self.max:                                                result=2**self.n                                                self.n+=1                                                return result                                else:                                                raise StopIteration
Here, __init__() is to take the value of max. Then, we create a python object ‘a’ of the class, with the argument 4.
Then, we create an iterator in Python using iter(). Next, we use the next() function to get the elements one by one.
>>> a=PowTwo(4) >>> i=iter(a) >>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
File “<pyshell#484>”, line 1, in <module>next(i)File “<pyshell#476>”, line 13, in __next__raise StopIteration
StopIteration
Alternatively, you can use the __iter__() and __next__() methods for this object.
>>> j=a.__iter__() >>> j.__next__()
Output
>>> j.__next__()
Output
>>> j.__next__()
Output
>>> j.__next__()
Output
>>> j.__next__()
Output
>>> j.__next__()
Output
File “<pyshell#491>”, line 1, in <module>j.__next__()File “<pyshell#476>”, line 13, in __next__
raise StopIteration
StopIteration
Internally, the iter() function calls the __iter__() method.
Infinite Python3 Iterator
It is indeed possible to create an iterator in Python that never exhausts. The iter() function can take another argument, called the ‘sentinel’, it keeps a watch.
As long as the first value isn’t the same as the sentinel, the Python iterator does not exhaust.
We know that the int() function, without a parameter, returns 0.
>>> int()
Now, we call iter() on two arguments- int and 1.
>>> a=iter(int,1)
This Python iterator will never exhaust; it is infinite. This is because 0 is never equal to 1.
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
And so on.
To create an infinite Python iterator using a class, take the following example.
>>> class Even:                def __iter__(self):                                self.num=2                                return self                def __next__(self):                                num=self.num                                self.num+=2                                return num       >>> e=Even() >>> i=iter(e) >>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
>>> next(i)
Output
This python iterates on even numbers beginning at 2, ending nowhere. So, you must be careful to include a terminating condition.
Benefits of Python Iterator
An iterator in python saves resources. To get all the elements, only one element is stored in the memory at a time. Unlike this, a list would have to store all the values at once.
Python Interview Questions on Iterators
- What are Python Iterators?
- Why Iterator is used in Python?
- How do you write an Iterator in Python?
- What is iterator and iterable in Python?
- Why is iteration important in Python?
Conclusion
In this article, we learned about python iterators. Aren’t they fun and super-handy? An iterator makes use of two functions- iter() and next().
However, we can make our own iterator in python with a python class. Finally, we looked at infinite iterators.
Your opinion matters
Please write your valuable feedback about DataFlair on Google
Hi,
In all the class definition, we are defining a method named __next__. However, we are calling a function named “next” not ” __next__”. I am getting an error while calling next on a class instance that we have defined.
How is it possible to define a method called “__next__” and calling next(i)?
Hi DM,
Here is the solution for your query on Python Iterator
The iterator object defines a __next__() method. This accesses elements in the container one at a time.
The built-in next() function is what we use to call the __next__() method on the iterator object.
Hope that clears your doubts. Keeping reading new blogs on Data Flair and share your experience with us.
Application level where to use iterators.. can you please explain with real time example..
Will suggest you to learn iterators with practicals through Free Python Course by DataFlair