Glossary

Iterable

An iterable is any object that can produce an iterator — an object that yields items one at a time. Technically, an iterable implements __iter__() (which returns an iterator) or __getitem__() (which allows index-based access as a fallback). Lists, tuples, strings, dictionaries, sets, files, range objects, and generators are all iterables.

The distinction between iterables and iterators is subtle but important. An iterable is a "source" that can be iterated over multiple times (like a list); an iterator is a "cursor" that tracks its position and can only be consumed once. Calling iter() on an iterable returns a fresh iterator. Calling iter() on an iterator returns itself. A for loop calls iter() implicitly, which is why you can loop over a list multiple times but can only loop over a generator once.

The iterable protocol is Python's universal abstraction for "a sequence of things." Anywhere Python expects a sequence — for loops, comprehensions, * unpacking, sum(), sorted(), min(), max(), list(), tuple(), set(), dict() — it accepts any iterable. This uniformity is one of Python's most powerful design decisions: by implementing __iter__, your custom class immediately works with all of Python's iteration machinery.

Related terms: Iterator, For Loop, Comprehension

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.