Python
Programming
Iterators
Iterables
Iteration

What are iterator, iterable, and iteration?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, the terms iterable, iterator, and iteration are related but not interchangeable. Understanding the difference makes loops, generators, and custom container types much easier to reason about. The short version is that an iterable can produce an iterator, an iterator produces values one at a time, and iteration is the act of consuming those values.

What an Iterable Is

An iterable is any object you can pass to iter(). Lists, tuples, strings, dictionaries, sets, files, and many custom classes are all iterable.

python
1numbers = [10, 20, 30]
2iterator = iter(numbers)
3
4print(iterator)

The list is the iterable. It is a collection that knows how to give you an iterator over its contents.

What an Iterator Is

An iterator is the object that actually yields the next value when next() is called. It also remembers where it currently is.

python
1numbers = [10, 20, 30]
2iterator = iter(numbers)
3
4print(next(iterator))
5print(next(iterator))
6print(next(iterator))

After the last value, calling next() again raises StopIteration.

python
1try:
2    print(next(iterator))
3except StopIteration:
4    print("iterator is exhausted")

This statefulness is the key distinction. A list does not track "current position" when you loop over it, but an iterator does.

What Iteration Means

Iteration is the process of consuming values one by one. A for loop does this automatically by calling iter() once and next() repeatedly until StopIteration occurs.

python
1numbers = [10, 20, 30]
2
3for value in numbers:
4    print(value)

Conceptually, Python turns that into something like:

python
1iterator = iter(numbers)
2
3while True:
4    try:
5        value = next(iterator)
6    except StopIteration:
7        break
8    print(value)

This is why for loops work with so many different object types. They rely on the iteration protocol rather than on list-specific behavior.

Iterables and Iterators Are Often Different Objects

A list is iterable, but it is not itself a one-shot iterator. Every call to iter(my_list) returns a fresh list iterator that starts from the beginning.

python
1numbers = [1, 2, 3]
2
3first = iter(numbers)
4second = iter(numbers)
5
6print(next(first))
7print(next(second))

Both iterators start independently at the first element.

By contrast, some objects are their own iterators. File objects and generators often behave that way. Once consumed, they do not automatically restart.

Generators Are Iterators

A generator function produces an iterator directly. That makes generators one of the most common iterator types in Python.

python
1def countdown(start):
2    while start > 0:
3        yield start
4        start -= 1
5
6gen = countdown(3)
7print(next(gen))
8print(next(gen))
9print(next(gen))

Each yield pauses the function and preserves its internal state for the next iteration step.

Building a Custom Iterable

You can make your own iterable by defining __iter__. The returned object should be an iterator.

python
1class CountUpTo:
2    def __init__(self, limit):
3        self.limit = limit
4
5    def __iter__(self):
6        current = 1
7        while current <= self.limit:
8            yield current
9            current += 1
10
11for number in CountUpTo(3):
12    print(number)

Using yield inside __iter__ is an easy way to create a valid iterable without writing a full custom iterator class by hand.

Why the Distinction Matters

This distinction matters whenever data may be consumed only once. If you pass a generator into one function and that function iterates through it, another function later may see nothing left. Lists and tuples are reusable iterables. Generators and many iterators are one-shot streams.

Knowing that difference helps prevent subtle bugs in data pipelines and utility functions.

Common Pitfalls

  • Calling something an iterator when it is really only an iterable collection.
  • Assuming every iterable can be restarted after partial consumption.
  • Forgetting that generators are usually exhausted after one full pass.
  • Writing code that calls next() without handling StopIteration.
  • Missing the fact that for loops hide the iterator protocol from view.

Summary

  • An iterable is an object you can loop over or pass to iter().
  • An iterator is the stateful object that returns values from next().
  • Iteration is the process of consuming values one by one.
  • Lists usually create fresh iterators, while generators are typically one-shot iterators themselves.
  • Understanding the distinction makes loops, generators, and custom container code much easier to reason about.

Course illustration
Course illustration

All Rights Reserved.