What is the difference between an iterable and an iterator?medium

Type
conceptual
Topic
iterable-iterator
Frequency
common
Tags
iterable, iterator, iteration, protocol
Answer

An iterable has __iter__ which returns an iterator. An iterator has both __iter__ and __next__. When you write for x in obj, Python calls iter(obj), then repeatedly calls next() until StopIteration is raised.

Explanation

The key distinction: iterators are stateful and exhaustible. A generator is an iterator — once exhausted it cannot be restarted. Lists are iterables but not iterators: each for loop creates a fresh list_iterator object, which is why you can loop over a list multiple times but not a generator. Use yield from to delegate to sub-iterators in generator pipelines, flattening nested iteration cleanly.

Follow-upWhy can an exhausted iterator not be reused directly?
Follow-upWhy can you loop over a list multiple times but not a generator?