Python Iterators
About Python Iterators
In Python, an iterator is an object that allows you to traverse through all the elements of a container (like a list, tuple, dictionary, or set) without needing to access the underlying structure directly.
Iterators are a fundamental concept in Python and are used extensively in loops and comprehensions.
An iterator implements the iterator protocol, which consists of two special methods: __iter__() and __next__().
- __iter__(): This method returns the iterator object itself. It is called once at the beginning of the iteration.
- __next__(): This method returns the next item from the iterator. When there are no more items to return, it raises a
StopIterationexception.
Getting an Iterator From an Iterable Object
An iterator can be created from an iterable object (like a list or a string) using the built-in iter() function.
Once you have an iterator, you can use the built-in next() function to retrieve the next item from the iterator.
Example of Creating an Iterator from a List
Explanation of the Code Above
color_list = ["red", "green", "blue"]: We create a list called color_list containing three color names.color_iterator = iter(color_list): We use theiter()function to get an iterator object from the color_list.next(color_iterator): We call thenext()function on the iterator to get the next item in the sequence. Each call tonext()returns the next color in the list until there are no more items (color) left, at that point aStopIterationexception is raised.
Iterators and for Loops
In Python, for loops are used to iterate over iterable objects like lists, tuples, and strings. Under the hood, a for loop automatically creates an iterator from the iterable object and uses it to traverse through the elements.
The for loop is the most common way to use iterators, and it handles all the details for you:
-
It calls
iter()on the iterable to get an iterator. -
In each loop, it calls
next()to get the next item. -
The
forloop automatically catches theStopIterationexception and ends the loop gracefully.
Creating Your Own Iterator (Custom Iterator)
You can create your own iterator by defining a class that implements the __iter__() and __next__() methods.
The steps to create a custom iterator are as follows:
- Define a class for your iterator.
- Implement the
__iter__()method to return the iterator object itself. - Implement the
__next__()method to return the next value and raiseStopIterationerror when there are no more values to return.
Let's create an iterator that counts up to a specified number.
Example of Creating a Custom Iterator
Generator Functions
Python offers a much simpler way to create iterators using generator functions.
A generator function is a function which contains the keyword yield. When called, it doesn't execute the function immediately; instead, it returns a special type of iterator called a generator.
Each time the generator's __next__() method is called, the function runs until it hits a yield statement, which provides a value and pauses the function's state. The next time __next__() is called, the function resumes right after the yield statement.
Let's create a generator function that counts up to a specified number.
Example of Creating a Generator Function
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Create your own iterators in the following code editor and click Execute button. Output will be displayed in Python Code Output section.
Python Editor