Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Understanding Iterators in Python

1 min read .
Understanding Iterators in Python

Iterators are a core Python concept behind for loops, generators, file objects, and many standard-library APIs. They provide one item at a time instead of requiring every result to be materialized in memory at once.

Iterable vs Iterator

An iterable is an object you can pass to iter(), such as a list, tuple, dictionary, set, or string.

An iterator is an object that produces successive values through next().

numbers = [1, 2, 3]
iterator = iter(numbers)

print(next(iterator))  # 1
print(next(iterator))  # 2
print(next(iterator))  # 3

Calling next() again raises StopIteration because the iterator is exhausted.

How for Uses Iterators

This loop:

for number in [1, 2, 3]:
    print(number)

roughly behaves like:

iterator = iter([1, 2, 3])

while True:
    try:
        number = next(iterator)
    except StopIteration:
        break
    print(number)

Normally, you should let for handle StopIteration for you.

Build a Custom Iterator

A custom iterator implements __iter__() and __next__():

class CountUpTo:
    def __init__(self, limit):
        self.current = 1
        self.limit = limit

    def __iter__(self):
        return self

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

        value = self.current
        self.current += 1
        return value


for number in CountUpTo(3):
    print(number)

Generators Are Often Simpler

For many custom iteration tasks, a generator function is easier to write and maintain:

def count_up_to(limit):
    current = 1
    while current <= limit:
        yield current
        current += 1


for number in count_up_to(3):
    print(number)

yield preserves the function’s state between iterations automatically.

Iterators Are Consumed

Many iterators can be traversed only once:

iterator = iter([1, 2, 3])

print(list(iterator))  # [1, 2, 3]
print(list(iterator))  # []

If you need repeated traversal, keep the original iterable or create a new iterator.

Why Iterators Matter

Iterators are useful when:

  • Processing large files line by line.
  • Streaming database or API results.
  • Building lazy data pipelines.
  • Avoiding unnecessary intermediate lists.

Conclusion

Iterators power Python’s iteration model. Learn the difference between iterables and iterators, remember that iterators are stateful and consumable, and prefer generator functions when you need a simple custom lazy sequence.

Related Posts

chevron-up