20. Python Iterators
Python Iterators
An iterator is an object that contains a countable number of values. It is an object that can be iterated upon, meaning you can traverse through all the values one by one.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().
Iterator vs Iterable
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have an iter() method which is used to get an iterator.
Even strings are iterable objects, and can return an iterator. When you use a for loop on a list or string, Python is actually creating an iterator behind the scenes and calling next() for you until it runs out of items.
Looping Through an Iterator
We can also use a for loop to iterate through an iterable object. The for loop automatically creates the iterator object and executes the next() method for each loop.
Create an Iterator
To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object.
- The __iter__() method must always return the iterator object itself. If required, it can also do some initialization.
- The __next__() method must return the next item in the sequence.
This iterator will produce numbers infinitely. If you put it in a for loop, it would run forever!
StopIteration
To prevent the iteration from going on forever, we can use the StopIteration statement. In the __next__() method, we can add a terminating condition to raise an error if the iteration is done.
Generators (The yield Keyword)
Building custom iterators with classes requires a lot of boilerplate code. Python provides Generators, which are a simple way of creating iterators using a regular function and the yield keyword.
When a function yields a value, it pauses its execution, saves its state, and returns the value. The next time next() is called, it resumes right where it left off.
Knowledge Check
Ready to test your understanding of 20. Python Iterators?