Learn Python Iterator with Syntax and Examples 2


1. Python Iterator

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. In this article, we will expand on python iterator. We will also learn what is an iterator, 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 Python iterator example.

2. Introduction to Python3 Iterators

Python Iterator - Introduction

Introduction to Python Iterator

An iterator in Python programming language is an object which you can iterate upon. That is, it returns one object at a time. Python3 Iterators are 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. Let’s revise Python Syntax and Python Method.

3. Building an Iterator in Python 3

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 evens 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 an Python3 iterator object. We store it in the variable evenIterator.

>>> evenIterator=iter(evens)
>>> evenIterator

<list_iterator object at 0x05E35410>

Remember, it does not have to be a list you create an iterator on.

>>> iter((1,3,2))

<tuple_iterator object at 0x05E35550>

Now, to access the first element, we apply the function next() on the iterator object.

>>> next(evenIterator)

2

We do the same for the next element(s) as well.

>>> next(evenIterator)

4

>>> next(evenIterator)

6

>>> next(evenIterator)

8

>>> next(evenIterator)

10

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)
Traceback (most recent call last):
File "<pyshell#442>", line 1, in <module>
next(evenIterator)

StopIteration

  • __next__()

You can also traverse the iterator using the __next__() method.

>>> nums=[1,2,3]
>>> numIter=iter(nums)
>>> numIter.__next__()

1

>>> next(numIter)

2

>>> numIter.__next__()

3

>>> numIter.__next__()
Traceback (most recent call last):
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)

[‘__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__’, ‘__subclasshook__’]

4. 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)

P

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

P

y

t

h

o

n

So, this is how the above for loop is actually implemented.

5. Building 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 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 using iter(). Next, we use the next() function to get the elements one by one.

>>> a=PowTwo(4)
>>> i=iter(a)
>>> next(i)

1

>>> next(i)

2

>>> next(i)

4

>>> next(i)

8

>>> next(i)

16

>>> next(i)
Traceback (most recent call last):
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__()

1

>>> j.__next__()

2

>>> j.__next__()

4

>>> j.__next__()

8

>>> j.__next__()

16

>>> j.__next__()
Traceback (most recent call last):
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.

6. Infinite Python3 Iterator

It is indeed possible to create an iterator 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 iterator does not exhaust.

We know that the int() function, without a parameter, returns 0.

>>> int()

0

Now, we call iter() on two arguments- int and 1.

>>> a=iter(int,1)

This iterator will never exhaust; it is infinite. This is because 0 is never equal to 1.

>>> next(a)

0

>>> next(a)

0

>>> next(a)

0

>>> next(a)

0

>>> next(a)

0

And so on.

To create an infinite 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)

2

>>> next(i)

4

>>> next(i)

6

>>> next(i)

8

This python iterates on even numbers beginning at 2, ending nowhere. So, you must be careful to include a terminating condition.

7. 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.

8. 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.

Top python Books to learn Python programming language.

For reference


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Learn Python Iterator with Syntax and Examples

  • DM

    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)?

    • Data Flair

      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.