Iterators

Published Nov 4, 2021Updated May 11, 2023
Contribute to Docs

In Python, an iterator is an object representing a collection of elements (such as data or methods) where each element can be accessed by traversing through the collection to perform the required tasks.

An iterator supports the next() function which takes no arguments and always returns the next element of the collection. When all elements are exhausted, it returns the StopIteration exception.

Iterables

Objects that can be converted into iterators are called iterables. Datatypes such as lists, tuples, dictionaries, and sets are iterable, meaning that they can be converted into iterators using the iter() function.

The difference between an iterable and an iterator can be easily be understood by considering iterable as a birthday cake that we need to distribute to a number of people. We cut cake into several pieces using the iter() function to get an iterator. Then we use next() to distribute each piece.

Also, it is important to understand that all iterators are iterable but the converse is not true.

Code for an Iterator using a List

The following code shows the generation of an iterator from a list using the iter() function and usage of the next() function by manually iterating through all the items of the iterator. When there are no more elements left in an iterator, it returns the StopIteration exception.

Note: Once an iterator has been iterated through, it cannot be re-iterated again.

Code
Output
Loading...

Iterator in a for loop

The for loop has built-in iter() and next() functions, which allows running iterations more elegantly.

Code
Output
Loading...

Iteration using a while loop

The while loop does not have the built-in iter() and next() functions as in the for loop, so the iter() and next() functions need to be used separately.

Code
Output
Loading...

Creating a User-Defined Iterable Object

Since an iterable object is an object that can be converted into Iterator, for an object to be iterable it needs to support the iter() function and the iterator generated must also support next().

Code
Output
Loading...

In the above code, built-in iter() function is equivalent to user-defined __iter__() method, when iter() function is invoked it invokes __iter__() method after checking the data type of input argument.

Similarly, next(cake_after_cutting) is same as cake_after_cutting.__next__(). When next() function is invoked it invokes __next__() method after checking data type of input argument.

Iterators

iter()
Returns an iterator object for a given object.

All contributors

Looking to contribute?

Learn Python on Codecademy