Python Iterators – Learn to create your own iterator in Python

Today we are going to learn about Python iterators. They are used everywhere in Python programming.

Chances are you might have already used them in loops, list comprehensions, or generators. It is important to study what iterators are and how you can create your own iterators.

We will cover everything about iterators in this article. So let’s get started.

What are Python Iterators?

In very simple words, Python iterators are objects that we can iterate upon. Iterating means to repeat a process multiple numbers of times. A single process is called an iteration.

In Python, we use iterators to repeat a process like traversing a sequence.

For example, a list can be traversed by reading one object at a time.

Iterators are used a lot in for-loops, lists comprehensions, and generators in Python.

Creating a Python Iterator

The iterator protocol in Python states that an iterable object must implement two methods: __iter__() and __next__().

First, we see how to create a Python iterator using Python built-in function iter(), and then, later on, we will create a Python iterator object from scratch.

The iter() function is a built-in function that is used to create an iterator that we can iterate using the next() function. First, we create a sequence of elements that we want to iterate.

Code:

nums = [2, 4, 1, 9, 6]

Then we use the iter() function to get an iterable object. The iter() function implicitly calls the __iter__() method.

Code:

nums_iter = iter( nums )
print( nums_iter )

The iter function, when used on lists, returns the list_iterator object.

Code:

<list_iterator object at 0x00000258BF9CE5C0>

The next() function is used to access the elements from the iterator one at a time.

Code:

print(next(nums_iter))

Output:

2

Now, when we call the next() function again, it will access the next element in the list.

Code:

print(next(nums_iter))

Output:

4

Like this, we can access all the elements of the list. But now, let’s see what will happen when the list comes to an end.

Code:

print(next(nums_iter))
print(next(nums_iter))
print(next(nums_iter))
print(next(nums_iter))

Output:

1
9
6
Traceback (most recent call last):
  File “<stdin>”, line 9, in <module>
StopIteration

As we can see, when we tried to access the next element, which does not exist, so the Python raises a StopIteration error.

Alternatively, you can also traverse the elements using the __next__() method, and it works the same as the next() function.

Code:

nums_iter = iter({1,2,3,4,5})

print(nums_iter.__next__())
print(nums_iter.__next__())
print(nums_iter.__next__())
print(nums_iter.__next__())
print(nums_iter.__next__())
print(nums_iter.__next__())

Output:

1
2
3
4
5
Traceback (most recent call last):
  File                                        “C:/Users/Techvidvan/AppData/Local/Programs/Python/Python38-32/test.py”, line 8, in <module>
    print(nums_iter.__next__())
StopIteration

The dir() function can be used to see the list of available methods on the iterable object.

Code:

dir(nums_iter)

Output:

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

For-loop for Iterators

You might have already used the for-loop in Python.

Iterators are working in the background of a for-loop. We can use the for-loop to iterate over the elements.

Code:

for city in ("Delhi", "Punjab", "Chennai", "Pune"):
  print(city)

Output:

Delhi
Punjab
Chennai
Pune

The for-loop automatically creates the iterator for the tuple and then in each iteration, it stores the current object in the city variable.

Lists, tuples, sets, strings, etc are all examples of iterable elements.

Code:

for character in "Hello!":
  print(character)

Output:

H
e
l
l
o
!

Create your Own Python Iterator

Now we will see how you can create your own iterables.

To create an iterator object on your own it is necessary to implement the __iter__() and __next__() methods. The __iter__() method will always return the iterator object itself. We can initialize variables in this method. The __next__() method is created to return the next element from the iterator. To reach the end we must raise the StopIteration error in this method.

Infinite Iterator

Let’s see an example:

Code:

class EvenNumbers:
  def __iter__(self):
    self.num = 0
    return self

  def __next__(self):
    next_num = self.num
    self.num += 2
    return self.num

evens = EvenNumbers()
even_iter = iter(evens)

print(next(even_iter))
print(next(even_iter))
print(next(even_iter))
print(next(even_iter))
print(next(even_iter))
print(next(even_iter))

Output:

2
4
6
8
10
12

Here, the class EvenNumbers is returning even numbers every time we call the next() function. This will iterate infinitely because we haven’t specified the condition when it needs to stop.

For creating iterators that we want to stop at certain conditions, we will implement the StopIteration exception in the __next__() method. We can then use a for loop to automatically iterate over all the elements.

Code:

class EvenNumbers:
  def __iter__(self):
    self.num = 0
    return self

  def __next__(self):
    if(self.num < 10):
        next_num = self.num
        self.num += 2
        return self.num
    else:
        raise StopIteration

evens = EvenNumbers()
even_iter = iter(evens)

for n in even_iter:
    print(n)

Output:

2
4
6
8
10

Benefits of Using Python Iterators

The main advantage of using the iterators is that the program is only holding one object at a time from a sequence or a collection.

For example, to perform an additional operation on each element of a huge list like [1334, 5534, 5345, 345, 144, ……. ]. We will only hold one value at a time to perform operations on. There is no need to keep all the elements of the list in the memory. This saves the resources of computers.

Summary

In this article, we have observed everything about iterators. We talked about what are Python iterators, how you can iterate the elements with for-loop and how you can create your own iterators.

Moreover, we understood the benefits of using iterators in your program.