Nearby lessons
113 of 159Python - Iterators
- Understand what an iterator is
- Know the difference between an iterable and an iterator
- Use the iterator protocol with __iter__() and __next__()
- Understand how the for loop uses iter() and next() internally
- Create a custom iterator class
What is an Iterator?
An iterator is an object that produces a sequence of values one at a time.
Instead of storing the entire sequence in memory, an iterator remembers its current position and produces the next value only when asked.
Iterators are the mechanism Python uses internally whenever a sequence is looped over.
Iterable vs Iterator
These two terms are often confused, but they are different.
| Feature | Iterable | Iterator |
|---|---|---|
| Meaning | An object that can be iterated over | An object that produces the values |
| Examples | List, tuple, string, dictionary, set | Result of iter(), generator, file object |
| Key Method | __iter__() |
__next__() |
| Can Be Reused | Yes | No, an iterator is exhausted after its values end |
The Iterator Protocol
In Python, any object is an iterator if it implements two methods. Together these two methods are called the Iterator Protocol.
__iter__()returns the iterator object itself.__next__()returns the next value, and raisesStopIterationwhen no more values remain.
The iter() and next() Functions
Python provides two built-in functions to work with iterators.
iter(iterable)converts an iterable into an iterator.next(iterator)fetches the next value from the iterator.
Every call to next() advances the iterator by one position.
Program: Converting a List into an Iterator
Explanation
Here, iter(l) converts the list l into an iterator i.
Each call to next(i) returns the next element of the list.
What is StopIteration?
When an iterator has no more values to produce, calling next() raises a StopIteration exception.
This exception signals that the iteration is complete.
How the for Loop Works Internally
When we write a for loop, Python performs the following steps internally:
- It calls
iter()on the iterable to obtain an iterator. - It repeatedly calls
next()on the iterator. - When
StopIterationis raised, the loop ends.
So a for loop over a list is internally an iterator using next().
Program: for Loop Internals
Explanation
This program manually replicates what a for loop does:
iter(l)creates the iterator.- The
while Trueloop keeps callingnext(i). - When
StopIterationis raised,breakends the loop.
Creating a Custom Iterator
We can create our own iterator by defining a class that implements both __iter__() and __next__().
The following class creates an iterator that produces the first n natural numbers.
Explanation
__init__()stores the limitnand initializescurrentto 0.__iter__()returnsself, which is the iterator itself.__next__()raisesStopIterationwhen the limit is reached, otherwise it returns the next number.- The
forloop works because the object implements the iterator protocol.
Advantages of Iterators
- Values are produced one at a time instead of storing the whole sequence.
- Very memory efficient for large sequences.
- They provide a uniform way to loop over any sequence.
- They are lazy: values are created only when requested.
- An iterator produces values one at a time instead of storing all of them in memory
- The iterator protocol uses __iter__() and __next__()
- iter() converts an iterable into an iterator, and next() fetches the next value
- The for loop calls iter() first, then next() repeatedly until StopIteration is raised
- Custom iterators are created by defining __iter__() and __next__() in a class