Python 3 Iterators and Generators

Python 3 Iterators and Generators


Iterators

Iteration is one of Python’s most powerful features, a way to access elements in a collection.

An iterator is an object that remembers the position of the traversal.

An iterator object starts at the first element in a collection and continues until all elements have been visited. Iterators can only move forward, not backward.

An iterator has two basic methods: iter() and next().

Strings, lists, or tuples can be used to create iterators:
Example (Python 3.0+)

>>> list=[1,2,3,4]<br>
>>> it = iter(list)    # Create an iterator object<br>
>>> print (next(it))   # Print the next element of the iterator<br>
1<br>
>>> print (next(it))<br>
2<br>
>>> <br>

Iterator objects can be iterated using a regular for statement:
Example (Python 3.0+)

#!/usr/bin/python3

list=[1,2,3,4]
it = iter(list) # Create an iterator object
for x in it:
print (x, end=" ")

Executing the above program will produce the following output:

1 2 3 4

You can also use the next() function:
Example (Python 3.0+)

#!/usr/bin/python3

import sys # Import the sys module

list=[1,2,3,4]
it = iter(list) # Create an iterator object

while True:
try:
print (next(it))
except StopIteration:
sys.exit()

Executing the above program will produce the following output:

1
2
3
4

Creating an Iterator

To use a class as an iterator, you need to implement two methods in the class: iter() and next().

If you’re familiar with object-oriented programming, you know that every class has a constructor. Python’s constructor is called init(), which is executed when the object is initialized.

For more information, see: Python 3 Object-Oriented Programming

The iter() method returns a special iterator object. This iterator object implements the next() method and signals the completion of iteration with the StopIteration exception.

The next() method (next() in Python 2) returns the next iterator object.

Create an iterator that returns numbers, starting with 1 and incrementing by 1:
Example (Python 3.0+)

class MyNumbers:
def __iter__(self):
self.a = 1
return self

def __next__(self):
x = self.a
self.a += 1
return x

myclass = MyNumbers()
myiter = iter(myclass)

print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))

Execution output:

 line-numbers">1
2
3
4
5

StopIteration

The StopIteration exception is used to signal the completion of an iteration, preventing infinite loops. In the next() method, we can set it to trigger a StopIteration exception after a specified number of iterations to terminate the iteration.

Stop execution after 20 iterations:
Example (Python 3.0+)

class MyNumbers:
def __iter__(self):
self.a = 1
return self

def __next__(self):
if self.a <= 20:
x = self.a
self.a += 1
return x
else:
raise StopIteration

myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
print(x)

The output of this execution is:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Generators

In Python, a function that uses yield is called a generator.

Unlike ordinary functions, a generator returns an iterator and can only be used for iteration. To put it simply, a generator is an iterator.

When a generator is called, each time a yield is encountered, the function pauses and saves all current execution information, returns the yielded value, and continues execution from the current position the next time the next() method is executed.

Calling a generator function returns an iterator object.

The following example uses yield to implement the Fibonacci sequence:
Example (Python 3.0+)

#!/usr/bin/python3

import sys

def fibonacci(n): # Generator function - Fibonacci
a, b, counter = 0, 1, 0
while True:
if (counter > n):
return
yield a
a, b = b, a + b
counter += 1
f = fibonacci(10) # f is an iterator returned by the generator

while True:
try:
print (next(f), end=" ")
except StopIteration:
sys.exit()

Execute the above program and the output is as follows:

0 1 1 2 3 5 8 13 21 34 55

Leave a Reply

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