Iterators in Python (original) (raw)

Last Updated : 5 Jun, 2026

An iterator in Python is an object used to traverse through all the elements of a collection (like lists, tuples or dictionaries) one element at a time. It follows the iterator protocol, which involves two key methods:

Need For Iterators

Here are some key benefits:

Built-in Iterator

Python provides built-in iterators for iterable objects such as strings, lists, tuples, and dictionaries. These iterators allow elements to be accessed one at a time using the next() function.

**Example: Let’s start with a simple example using a string. We will convert it into an iterator and fetch characters one by one:

Python `

s = "GFG" it = iter(s)

print(next(it)) print(next(it)) print(next(it))

`

**Explanation:

Creating a Custom Iterator

Creating a custom iterator in Python involves defining a class that implements the __iter__() and __next__() methods according to the Python iterator protocol.

**Steps to follow:

Below is an example of a custom class called EvenNumbers, which iterates through even numbers starting from 2:

Python `

class EvenNumbers: def init(self, limit): self.limit = limit self.n = 2

def __iter__(self):
    return self

def __next__(self):
    if self.n > self.limit:
        raise StopIteration

    x = self.n
    self.n += 2
    return x

Create an iterator for even numbers up to 10

even = EvenNumbers(10)

for num in even: print(num)

`

**Explanation:

StopIteration Exception

StopIteration exception is integrated with Python’s iterator protocol. It signals that the iterator has no more items to return. Once this exception is raised, further calls to next() on the same iterator will continue raising StopIteration.

**Example:

Python `

li = [100, 200, 300] it = iter(li)

Iterate until StopIteration is raised

while True: try: print(next(it)) except StopIteration: print("End of iteration") break

`

Output

100 200 300 End of iteration

In this example, the StopIteration exception is manually handled in the while loop, allowing for custom handling when the iterator is exhausted.

Iterator vs Iterable

Although the terms iterator and iterable sound similar, they are not the same. An iterable is any object that can return an iterator, while an iterator is the actual object that performs iteration one element at a time.

**Example: Let’s take a list (iterable) and create an iterator from it

Python `

Iterable: list

numbers = [1, 2, 3]

Iterator: created using iter()

it = iter(numbers) print(next(it)) print(next(it))
print(next(it))

`

**Explanation:

To make the difference even clearer, let’s summarize it in a simple table:

Feature Iterable Iterator
Definition Any object that can return an iterator Object with a state for iteration
Key Method Implements __iter__() Implements both __iter__() and __next__()
Examples List, Tuple, String, Dictionary, Set Objects returned by iter()
Can use next() directly? No Yes