Python Iterators

By | July 13, 2019

In this tutorial, we will learn what are iterators in python and where we use them. We will also discuss how can we create our own Iterators using __iter__ and __next__ methods.

What are Iterators?

Iterators are the objects in python which are used to iterate over a collection of values. The most common iterator in Python is for loop. A for loop statement iterate through iterable such as string, List, tuple and Dictionary. By iterating mean, it traverses every value of the iterable. When we use a for iterator, we see that it returns one value at a time because iterators can return one value.

Though for loop is an inbuilt iterator in python, according to the Python iterator protocol each iterator should have 2 magical methods __iter__() and __next__().

Each iterator needs iterable to iterate upon and python has built-in iterable like string, List, Tuples and Dictionaries.

Iterating Through an Iterator:

To iterate through an Iterable we can use two iterator functions next() and iter().

The iter() function invoke the __iter__() method and returns an iterator.

The next() function is used to iterate upon the iterator. The next() function invoke all the values of iterator one by one and if we call the next() function when there is no value left to iterate then it would throw a StopIteration error.

Example:

my_iterable = [1,2,3,4,5]               #we can not perform next() function on iterables
create_iterator = iter(my_iterable)          #it return a iterator with values
print(next(create_iterator))        #the next poiner is at 1 index
print(next(create_iterator))        #the next pointer is at 2 index
print(next(create_iterator))        #the next pointer is at 3 index
print(next(create_iterator))        #the next pointer is at 4 index
print(next(create_iterator))        #the next pointer is at 5 index
print('------------------After Iterating through all values-----------------')
print(next(create_iterator))        # there are no more values present in the iterator to iterate so it will throw an error

Output:

1
2
3
4
5
------------------After Iterating through all values-----------------
Traceback (most recent call last):
StopIteration 

Behind the Code:

In the above code first, we use the iter() function to create an iterator which we assigned to a variable name create_iterator. Then we use the next() function to iterate through every value of that iterator. In the above example, we can see that every time the next() function returning the next value of the iterator and when it iterates through all the value and there is no more value left to iterate it throw an error.

Note: We can perform the same action using a for loop and without an error.

How does for loop work?

for loop is an inbuilt python iterator and can iterate through any kind of iterable. For loop also use the iter() and next() method to perform the iteration.

For loop Syntax:

for values in iterable:
    pass

For Loop Inner Working:

iterator_object= iter(iterable)
while True:
    try:
        values= next(iterator_object)
    except StopIteration:    #Once all the values are iterated
        break

Building Our Own Iterators

With the help of __iter()__ and __next__() methods we can create our own iterators.

Example:

class multhree:
"""this class create an iterator of multiple of three"""

    def __init__(self,m=0):
       self.m=m

    def __iter__(self):
        self.n=1
        return self

    def __next__(self):
        if self.n <=self.m:
            r = 3*self.n
            self.n+=1
            return r
        else:
            raise StopIteration("NO more elements to iterate")

c_o= multhree(6)
i =iter(c_o)
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))

Output:

3
6
9
12
15
18
Traceback (most recent call last):
StopIteration: NO more elements to iterate

Python Infinite Iterators

While creating an Iterator make sure that it must terminate at some value. What if we do not give our iterator a terminating point it will become an infinite Iterator which will have infinite values to access.

Example:

class multhree:
  """this class create an iterator of multiple of three"""
    def __init__(self,m=0):
        self.m=m

    def __iter__(self):
        self.n=1
        return self
           
    def __next__(self):
        r = 3*self.n
        self.n+=1
        return r                         

c_o= multhree(6)
i =iter(c_o)
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))
print(next(i))

Output:

3
6
9
12
15
18
21

Behind the code:

Here in the above program we have passed 6 as an argument to the class multhree() and we were only hoping for 6 multiples of three but the iterator is generating more values as we wanted this can cause many problems.

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

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