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.
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.
After the last value, calling next() again raises StopIteration.
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.
Conceptually, Python turns that into something like:
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.
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.
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.
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 handlingStopIteration. - Missing the fact that
forloops 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.

