askvity

How to Iterate Over an Iterator in Python?

Published in Python Iteration 3 mins read

You can iterate over an iterator in Python primarily using a for loop or by manually calling the next() function.

Understanding Iterators

An iterator is an object that produces values one at a time. It implements the iterator protocol, which requires two methods:

  • __iter__(): Returns the iterator object itself. This is used when the iterator is used in a for loop.
  • __next__(): Returns the next value from the iterator. If there are no more values, it raises a StopIteration exception.

Iteration Methods

Here's how you can iterate through an iterator:

1. Using a for Loop

The for loop is the most common and Pythonic way to iterate over an iterator. The loop automatically handles the calls to next() and catches the StopIteration exception.

my_list = [1, 2, 3]
my_iterator = iter(my_list)

for item in my_iterator:
    print(item)  # Output: 1 2 3

In this example:

  • iter(my_list) creates an iterator from the list my_list.
  • The for loop automatically calls next(my_iterator) in each iteration until a StopIteration exception is raised.

2. Using the next() Function Manually

You can also use the next() function to retrieve values from the iterator one at a time. This method gives you more control, but you need to handle the StopIteration exception yourself.

my_list = [1, 2, 3]
my_iterator = iter(my_list)

try:
    print(next(my_iterator))  # Output: 1
    print(next(my_iterator))  # Output: 2
    print(next(my_iterator))  # Output: 3
    print(next(my_iterator))  # Raises StopIteration
except StopIteration:
    print("End of iterator") # Output: End of iterator

In this example:

  • Each call to next(my_iterator) retrieves the next element.
  • When the iterator is exhausted, next(my_iterator) raises a StopIteration exception, which is caught by the try...except block.

3. Combining while loop with next()

Another option is to use a while loop, handling the StopIteration exception as the exit condition.

my_list = [1, 2, 3]
my_iterator = iter(my_list)

while True:
    try:
        item = next(my_iterator)
        print(item)
    except StopIteration:
        break # Exit the loop when StopIteration is raised

This approach achieves the same outcome as the for loop but requires explicit handling of the StopIteration exception.

Considerations

  • Exhaustion: Iterators are "exhausted" once they have yielded all their values. After exhaustion, calling next() will always raise a StopIteration exception unless the iterator is designed to reset itself (which is uncommon).
  • Stateful: Iterators maintain their state. Unlike iterable objects (e.g., lists) which can be iterated over multiple times, an iterator can only be iterated once unless it's specifically designed to reset or create a new iterator.
  • Memory Efficiency: Iterators are memory-efficient for large datasets because they generate values on demand instead of storing them all in memory at once.

In summary, Python offers several ways to iterate over an iterator, with the for loop being the most concise and commonly used method. Using next() provides more control, but requires explicit exception handling.

Related Articles