Python iterators

In Python, iterators are special objects that allow you to efficiently loop through elements in a collection one at a time. They provide a controlled way to access items in sequences like lists, tuples, strings, dictionaries (keys), and sets.

Iterables

These are data structures that can be used with a for loop. Examples include lists, tuples, strings, dictionaries (keys), and sets.

Iterators

These are objects that implement the iterator protocol, which consists of two special methods:

__iter__()

This method returns the iterator object itself. It's called implicitly when you use an iterable in a for loop.

__next__()

This method is called repeatedly to retrieve the next element from the iterator. It raises a StopIteration exception when there are no more elements left.

Creating and Using Iterators

Built-in iter() Function:

The iter() function takes an iterable as input and returns its corresponding iterator. You can then use the next() method on the iterator to access elements.


my_list = [1, 2, 3, 4]
list_iterator = iter(my_list)

print(next(list_iterator))  # Output: 1
print(next(list_iterator))  # Output: 2
# ... (you can continue calling next() to iterate)

Custom Iterators:

You can create your own custom iterators by defining a class that implements the __iter__() and __next__() methods.


class NumberGenerator:
    def __init__(self, start, stop):
        self.start = start
        self.stop = stop
        self.current = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current <= self.stop:
            current_value = self.current
            self.current += 1
            return current_value
        else:
            raise StopIteration

for num in NumberGenerator(1, 5):
    print(num)  # Output: 1 2 3 4 5

Benefits of Iterators

Understanding for Loops

Behind the scenes, Python's for loop leverages iterators. When you use an iterable in a for loop:

In Summary

Iterators are a fundamental concept in Python for working with sequences efficiently. They provide a mechanism for controlled iteration, memory optimization, and customization of how elements are accessed. By understanding iterators, you can write more memory-efficient and versatile code for processing sequences in Python.