06 / 08

What is the difference between an iterable and an iterator? What protocol must an object implement to be an iterator?

Difficulty: 5/10
Iterators, Iterator Protocol, Iterables

Iterable produces iterators; iterators implement iter and next

An iterable is any object that can produce an iterator: it implements iter, which returns a fresh iterator. An iterator is the object that does the actual work: it implements next, which returns the next value or raises StopIteration to signal exhaustion, and iter, which returns self so that an iterator is itself iterable. The for loop is just sugar: Python calls iter(obj) once, then next() repeatedly until StopIteration is raised. The critical design consequence is that iterators are stateful and single-use, while well-behaved iterables are re-iterable because each iter call returns a new iterator.

  1. 1

    Iterable: defines iter (or the legacy getitem fallback). Lists, dicts, sets, strings, and file objects are iterables.

  2. 2

    Iterator: defines next and iter returning self. Generators, map/filter/zip results, and reversed objects are iterators.

  3. 3

    Trade-off: one-shot iterators are memory-efficient and lazily evaluated, but if you need two passes you must either keep the iterable and call iter() again or materialize into a list, which trades memory for reusability.

  4. 4

    Common mistake: implementing only next on a class. The for loop calls iter() first, so a next-only object raises TypeError: object is not iterable.

  5. 5

    Common mistake: returning self from iter on a container class, which makes the second loop over the same object silently empty.

  6. 6

    Version note: PEP 234 defined the iter/next protocol. The next method was called next() in Python 2, and the getitem iteration fallback still works but is legacy.

javascript

Scenario Questions

0-2 years experience

  1. 1Why does for x in {'a': 1} give keys, while for x in 5 raises TypeError?
  2. 2You wrote a class with a __next__ method but no __iter__. Why does for x in obj fail?

2-5 years experience

  1. 1You need to iterate a custom object twice inside the same function. What do you implement and what must it return?
  2. 2A teammate's class returns self from __iter__ and the second loop is empty. How do you fix it without changing call sites?

5-8 years experience

  1. 1You are building a paginated API client. Should the page fetcher be an iterable or an iterator, and why?
  2. 2You need to expose an infinite stream of sensor readings that also supports reset() and replay-from-start. How do you design it?

8+ years experience

  1. 1Design a zero-copy columnar reader that is iterable, supports multiple independent cursors, and is safe under concurrent reads.
  2. 2You need your type to work with for loops, tuple unpacking, and itertools without special-casing. What is the minimal protocol contract and what are the edge cases?

Follow-up Questions

  • How do you make a one-shot generator re-iterable without holding all items in memory at once?
  • What happens if __iter__ on a container class returns self instead of a new iterator?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.