Interview Q&A · Python · 20 questions in this topic

Iterators & Generators — Python Interview Q&A

All Python topics

The iterator protocol, yield, lazy evaluation and the memory arguments that decide between a generator and a list. Part of Iterators & Generators · 375 questions in the Python bank.

What is an iterable, and how is it different from an iterator?

Answer

An iterable is any object you can loop over (it implements __iter__, which returns an iterator). An iterator is the object that actually does the iterating — it implements __next__ and remembers its current position, produced by calling iter() on an iterable.

Every iterator is also an iterable (its __iter__ typically returns itself), but not every iterable is an iterator — a list is iterable but is not itself an iterator, since it doesn't track position; calling iter() on it produces a separate list-iterator object that does.

Example

Example.pypython
nums = [1, 2, 3] # iterable -- has __iter__
print(hasattr(nums, "__iter__")) # True
print(hasattr(nums, "__next__")) # False -- list itself is NOT an iterator
it = iter(nums) # calling iter() produces an iterator
print(hasattr(it, "__next__")) # True
print(next(it)) # 1
print(next(it)) # 2

💡 The one-sentence distinction to nail: "an iterable knows how to produce an iterator; an iterator knows how to produce the next value and remembers where it left off." Most confusion comes from treating the two as synonyms.

What does the iter() function do?

Answer

iter(obj) returns an iterator for obj by calling its __iter__ method internally. It's what a for loop calls automatically, once, before it starts repeatedly calling next().

iter() also supports a lesser-known two-argument form, iter(callable, sentinel), which repeatedly calls callable() until it returns a value equal to sentinel — a pattern occasionally used for reading fixedsize chunks or polling until a stop condition.

Example

Example.pypython
nums = [10, 20, 30]
it = iter(nums)
print(type(it)) # <class 'list_iterator'>
# The two-argument "sentinel" form
import random
random.seed(1)
def roll():
    return random.randint(1, 6)
for value in iter(roll, 6): # keeps rolling until it gets a 6
    print(value)

💡 Mentioning the two-argument iter(callable, sentinel) form is a nice "hidden gem" detail — most developers only ever use the single-argument version.

What does next() do, and what happens when an iterator is exhausted?

Answer

next(iterator) retrieves the next value from an iterator by calling its __next__ method. Once there are no more values, the iterator raises StopIteration — which is exactly the signal a for loop relies on internally to know when to stop.

next() accepts an optional second argument as a default value to return instead of raising StopIteration, which is useful when you want to consume "one more item, or a fallback" without wrapping the call in a try / except.

Example

Example.pypython
it = iter([1, 2])
print(next(it)) # 1
print(next(it)) # 2
# print(next(it)) # StopIteration!
print(next(it, "no more items")) # "no more items" -- default avoids the exception

💡 Point out that a for loop is essentially syntactic sugar for repeatedly calling next() inside a try / except StopIteration: block — this connects the abstract protocol to the everyday syntax everyone already uses.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits