# Iterators Protocol

> The iterator protocol is a fundamental concept in Python that defines how objects support iteration. Any object that implements the iter() and next()...

Canonical: https://www.internhack.in/learn/python/advanced-python/iterators-protocol
Last updated: 2026-11-28

Difficulty: Advanced

The iterator protocol is a fundamental concept in Python that defines how objects support iteration. Any object that implements the \__iter_\_() and \__next_\_() methods follows the iterator protocol and can be used in for loops, comprehensions, and anywhere an iterable is expected.

An iterable is any object that implements \__iter_\_() and returns an iterator. An iterator is an object that implements both \__iter_\_() (returning itself) and \__next_\_() (returning the next value or raising StopIteration when exhausted). Lists, tuples, dicts, and strings are all iterables -- calling iter() on them returns an iterator.

To create a custom iterator, you define a class with \__iter_\_() and \__next_\_() methods. The \__iter_\_ method returns the iterator object (usually self), and \__next_\_ returns the next value in the sequence. When there are no more values, \__next_\_ must raise StopIteration to signal that iteration is complete.

Understanding the iterator protocol clarifies how Python's for loop works under the hood. When you write \`for item in obj\`, Python calls iter(obj) to get an iterator, then repeatedly calls next() on that iterator until StopIteration is raised. This mechanism powers list comprehensions, unpacking, and all other iteration contexts.

Custom iterators give you fine-grained control over iteration behavior. Unlike generators (which are simpler to write), class-based iterators can maintain complex state, support reset operations, and implement additional methods. They're essential when you need reusable, configurable iteration objects.

## Code examples

### How for Loops Use the Iterator Protocol

```
numbers = [10, 20, 30]
iterator = iter(numbers)

print(next(iterator))
print(next(iterator))
print(next(iterator))
```

iter() calls \__iter_\_ on the list to get an iterator. Each next() call invokes \__next_\_ on the iterator, returning values one at a time.

### Custom Iterator Class

```
class CountDown:
    def __init__(self, start):
        self.current = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.current <= 0:
            raise StopIteration
        value = self.current
        self.current -= 1
        return value

for num in CountDown(5):
    print(num)
```

CountDown implements \__iter_\_ (returns self) and \__next_\_ (returns current value and decrements). The for loop calls these automatically and stops when StopIteration is raised.

### Iterable vs Iterator

```
class EvenNumbers:
    def __init__(self, limit):
        self.limit = limit

    def __iter__(self):
        self.current = 0
        return self

    def __next__(self):
        if self.current >= self.limit:
            raise StopIteration
        value = self.current
        self.current += 2
        return value

evens = EvenNumbers(10)
print(list(evens))
print(list(evens))
```

Because \__iter_\_ resets self.current to 0 each time, this object can be iterated multiple times. This is a common pattern for making reusable iterables.

### Manual Iteration with StopIteration Handling

```
class TwoItems:
    def __init__(self, a, b):
        self.items = [a, b]
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.index >= len(self.items):
            raise StopIteration
        value = self.items[self.index]
        self.index += 1
        return value

it = TwoItems("hello", "world")
print(next(it))
print(next(it))
try:
    next(it)
except StopIteration:
    print("No more items")
```

After yielding both items, \__next_\_ raises StopIteration. We catch it to demonstrate what happens when a for loop encounters the end of an iterator.

## Key points

- Every iterator is an iterable (has \__iter_\_), but not every iterable is an iterator (lists have \__iter_\_ but not \__next_\_).
- Calling iter() on a list returns a list_iterator object -- the list itself is not the iterator.
- Resetting iteration state inside \__iter_\_ (like self.current = 0) allows the object to be iterated multiple times.

## Interview tips

- Clearly distinguish between iterables (have \__iter_\_) and iterators (have both \__iter_\_ and \__next_\_). A list is iterable but not an iterator.
- Be prepared to implement a custom iterator class from scratch and explain how Python's for loop desugars into iter() and next() calls.

## Concepts covered

\__iter_\_, \__next_\_, custom iterators, StopIteration

Previous: [Generators](https://www.internhack.in/learn/python/advanced-python/generators) · Next: [Context Managers](https://www.internhack.in/learn/python/advanced-python/context-managers) · [All of Advanced Python](https://www.internhack.in/learn/python/advanced-python)

## More Advanced Python lessons

- [Closures & Functional Programming](https://www.internhack.in/learn/python/advanced-python/closures-and-functional)
- [Decorators](https://www.internhack.in/learn/python/advanced-python/decorators)

## Related

- [Learn](https://www.internhack.in/learn)
- [Python](https://www.internhack.in/learn/python)
- [Advanced Python](https://www.internhack.in/learn/python/advanced-python)
