Iterator
In one line
An iterator is an object that produces values one at a time using __next__(), enabling lazy evaluation of sequences.
In simple words
An iterator is an object that represents a stream of data — it produces values one at a time on demand. You get the next value with next(), and the iterator raises StopIteration when exhausted.
Lists, strings, dicts, and files are iterable (you can loop over them), but they are not iterators themselves. You create an iterator from an iterable with iter(). The for loop does this automatically.
Iterators are lazy — they don't compute all values upfront. This makes them memory-efficient for large or infinite sequences. Generators are a convenient way to create iterators using yield.
Example
nums = [1, 2, 3]
it = iter(nums)
print(next(it)) # 1
print(next(it)) # 2
print(next(it)) # 3
# next(it) would raise StopIteration
# for loop uses iter() internally
for n in nums:
print(n)Iterators produce values one at a time with next(). iter() creates an iterator from an iterable.
Common confusions
Confused with: iterable
The difference: An iterable is any object you can loop over (list, string, dict) — it has __iter__. An iterator is the object that actually produces values one at a time — it has __next__. Every iterator is iterable, but not every iterable is an iterator.
Related terms
Related Resources
Related Lessons
- Iterators
Iterators in Python.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.