Python Iterators

Video Tutorial
FREE
Iterator Iterable & Iteration thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Python is an object-oriented language; everything in Python is considered an object, including variables, functions, lists, tuples, sets, etc. Some objects are iterable, which means we can traverse over them, and they will return their member value individually.

Examples of iterable objects are: List, Tuple, Dictionary

Just like all other things, iterators in Python are also objects. They are used for iterating over iterable objects. This means that we can use iterators on lists, tuples, dictionaries, and strings.

Iterators are implemented using two unique Python methods iter() and next(). They are collectively called iterator protocols.

Python iter() Example

Here is the code explaining how to use iterators in Python.

Output:

Iterator vs Iterable

With so much programming jargon, it is easy to forget the difference between an Iterator and an Iterable.

Here is a simple explanation.

An iterator is an object that consists of __iter__() and __next__() (collectively these are known as iterator protocol).

An iterable is anything we can loop over using a loop.

This is the easiest explanation that will help you differentiate between the two.

Looping through an Iterator

for loop in Python can be used to iterate over any iterable. To better understand iterators,, let’s see how iterators are used inside of for loop in python.

The syntax of for loop is:

This is what happens behind the scenes

Output:

In this code, we made an iterator object of the iterable nums.

Then we made an infinite while loop in which there is a try-catch block where we stop the loop if we get StopIteration; else, it will continue to print the next element in the iterable.

Create an Iterator

Like any other thing in Python, making a custom iterator from scratch is easy. To make our own iterator, we have to use two dunder/magic methods provided by Python.

Dunder/Magic methods are methods that have two underscores before and after their name; their invocation generally happens internally from a class or some action.

The two dunder methods we have to use are:

  1. __iter__()
  2. __next__() These are similar to iter() and next().

__iter__() should return the iterator object. If required, some initialization can be performed.

__next__() should return the next item in the sequence. Upon reaching the end, it should raise StopIteration.

Here is the code showing how to do it

Output:

Let’s understand what is happening here.

We made a class ModOfTwo, which will give us the mod of the numbers starting from 0 to the number (which is 3 in our case).

In the init(), we defined a maximum number; this is the number till which we want to calculate mods.

In the __iter__(), we are setting the value of a variable n to 0. We will return the mod of this variable in the __next__().

In `next(), we first check if the value of n is less than max or not; otherwise, the iterator will become an infinite iterator. After the check, we calculate and return the mod and increment the n value by 1.

StopIteration

The above code will work fine, but if we add one more print statement, then we will get this error:

This occurred because if the n is greater than max, then a StopIteration exception is raised in the code we have defined.

Python Infinite Iterators

Iterators iterate over an iterable (lists, tuples, strings, etc.), and they are finite or have a fixed length. But iterators can also be infinite or never-ending.

These Python iterators are capable of running forever, and special care needs to be taken while handling them.

Let’s understand Infinite Iterators by making our own infinite iterator.

This will be very similar to the custom iterator(ModOfTwo) we made earlier.

Output:

This code is very similar to the code we wrote while making our custom iterator. Instead of calculating the mod here, we are calculating the multiples of 2.

The main difference is we do not have the max variable we used in the next(). The max variable there was responsible for raising an exception if all the values were traversed. In this code, we are not performing any such check. Because of the absence of that check, this iterator is infinite. It will keep returning you the multiples of 2 forever.

Conclusion

In this article, we covered a lot about iterators in Python.

Iterators are very helpful, and they help us save a lot of resources. As we can see in finding the multiples of two, we are not storing any array of numbers.

Here are a few key points that you should remember

  • Iterators are Python objects which are used to iterate over iterable objects.
  • iter() and next() can be used to iterate over iterable objects
  • __iter__() and __next__() are used to create custom iterators
  • for loop in Python, use iterators behind the scene
  • Infinite iterators are capable of returning infinite values.

See Also