English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python Iterators

Iterators are objects that are iterable. In this tutorial, you will learn the working principle of iterators and how to build your own iterators using the __iter__ and __next__ methods.

What is an iterator in Python?

Iterators are everywhere in Python. They are elegantly implemented in for loops, comprehensions, generators, etc., but they are hidden right under our noses.

An Iterator in Python is just an object that is iterableobject. One will return the data object, returning one element at a time.

Technically, Python iterator objectTwo special methods must be implemented, __iter__() and __next__(), collectively known asiterator protocol.

If we can obtain an iterator from an object, then the object is callediterableiteration. Most built-in containers in Python (such as:listtuplestringetc.) are iterable.

The iter() function (also known as the __iter__() method) returns an iterator from them.

Iterating through iterators in Python

We use the next() function to manually traverse all items of the iterator. When we reach the end and there is no more data to return, it will raise StopIteration. Here is an example...

# Define a list
my_list = [4, 7, 0, 3]
# Obtaining an iterator using iter()
my_iter = iter(my_list)
## Obtaining an iterator using iter() 
#output 4
print(next(my_iter))
#output 7
print(next(my_iter))
## next(obj)is the same as obj .__ next __()
#output 0
print(my_iter.__next__())
#output 3
print(my_iter.__next__())
## This will cause an error, as there are no items left
next(my_iter)

A more elegant way to perform automatic iteration is to usefor loopUsing this method, we can iterate over any object that can return an iterator, such as lists, strings, files, etc.

>>> for element in my_list:
...     print(element)
...     
4
7
0
3

How does the for loop actually work?

As we saw in the example above, the for loop can automatically traverse a list.

In fact, the for loop can iterate over any iterable object. Let's take a close look at how the for loop is actually implemented in Python.

for element in iterable:
    # Do something with the element

It is actually implemented as.

# Create an iterator object iterable
iter_obj = iter(iterable)
# Infinite loop
while True:
    try:
        # Get the next item
        element = next(iter_obj)
        # Do something with the element
    except StopIteration:
        # If StopIteration is raised, break out of the loop
        break

Therefore, internally, the for loop creates an iterator object iter_obj by calling iter() on the iterable.

Ironically, this for loop is actually infiniteWhile loop.

Inside the loop, it calls next() to get the next element and uses this value to execute the main body of the for loop. When all items are finished, StopIteration is thrown, which is caught internally, and the loop ends. Note that any other type of exception will pass through.

Build your own iterator with Python

It is easy to build an iterator from scratch in Python. We just need to implement these methods __iter__() and __next__().

__iter__() method returns the iterator object itself. It can perform some initialization if necessary.

__next__() method must return the next item in the sequence. It must raise StopIteration when reaching the end and in subsequent calls.

Here, we show an example that will provide us with2The power of. The power index ranges from 0 to the number set by the user.

class PowTwo:
    """Implement the iterator class"
             "The power of 2"
    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

Now, we can create an iterator and iterate as follows.

>>> 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):
...
StopIteration

We can also use a for loop to iterate through the iterator class.

>>> for i in PowTwo(5) :
... print(i)
...     
1
2
4
8
16
32

Python infinite iterator

The items in the iterator object do not have to be exhausted. There may be infinite iterators (that will never end). When dealing with such iterators, we must be careful.

This is a simple example demonstrating the infinite iterator.

Built-in functions iter() can be called with two parameters, where the first parameter must be a callable object (function), and the second parameter is the marker. The iterator calls this function until the returned value equals the marker value.

>>> int()
0
>>> inf = iter(int,1)
>>> next(inf)
0
>>> next(inf)
0

We can see that the int() function always returns 0. Therefore, it is used as iter(int,1) passing will return an iterator that calls int() until the returned value equals1. This will never happen, and we get an infinite iterator.

We can also build our own infinite iterators. Theoretically, the following iterator will return all odd numbers.

class InfIter:
    """The infinite iterator returns all
                 odd numbers """
    def __iter__(self):
        self.num = 1
        return self
    def __next__(self):
        num = self.num
        self.num += 2
        return num

Run as follows.

>>> a = iter(InfIter())
>>> next(a)
1
>>> next(a)
3
>>> next(a)
5
>>> next(a)
7

...

Be careful to include a termination condition when iterating over these types of infinite iterators.

The advantage of using iterators is that they save resources. As shown above, we can get all odd numbers without having to store the entire number system in memory. In theory, we can include an infinite number of items in a finite amount of memory.

Iterators also make our code look cool.

There is an easy way to create an iterator in Python. For more information, please visit:Python generator yield.