An iterable can provide an iterator; an iterator produces values one at a time and keeps track of its current position. A generator is a convenient way to write an iterator whose execution pauses at yield. Interview questions often test when work happens and what remains after a value has been consumed.

This guide uses synchronous Python 3 generators. Async iteration is a separate protocol and is not needed to understand the examples here.

When does a generator function run?

Calling it creates a generator object. Its body starts when iteration requests a value. Reaching yield returns a value to the caller and suspends the function, keeping enough state to continue later.

def high_readings(readings, threshold):
    for reading in readings:
        if reading > threshold:
            yield reading

source = iter([18, 24, 21, 29])
high = high_readings(source, 22)
assert next(high) == 24
assert list(high) == [29]
assert list(high) == []
assert list(source) == []

The first request scans until 24 qualifies. The next conversion to a list continues from that point and finds 29. A completed generator does not restart because another loop uses it. Create a new generator with a new input if you need a fresh traversal.

How is a list different from its iterator?

A list stores its elements and can normally provide a fresh iterator for each new traversal. An iterator represents an ongoing traversal. Calling iter on an iterator returns that iterator rather than rewinding it.

This difference matters when a function receives an unknown iterable. Counting it with one loop can exhaust a one-pass input before a second loop tries to process it. Decide whether your function needs one pass, several passes or a materialized collection, and document that requirement.

Does lazy processing always use little memory?

No. A generator does not automatically make every part of the pipeline bounded. It can still keep a large local object, retain its input or call an operation that builds a complete collection. Sorting all produced values requires retaining values even if the input was generated lazily.

For a stream of readings, filtering and counting in one pass can avoid storing every reading. If the source is already a list, however, that input list still occupies memory. Say which allocation you are avoiding rather than promising that a generator removes all memory cost.

How should exhaustion be handled?

A normal for loop handles the iterator protocol for you. When asking for values directly, next(iterator, default) can provide a clear result for exhaustion. Choose a default that cannot be confused with an ordinary item, or use a separate sentinel.

empty = iter([])
missing = object()
assert next(empty, missing) is missing
assert list(high_readings([10, 11], 22)) == []

Empty output can be a valid result. It does not necessarily mean the iterator failed. For error handling, distinguish “no item matched” from an exception raised while fetching or interpreting an item.

What changes when the producer has cleanup work?

A generator that manages a resource needs deliberate lifetime handling. Stopping a consumer early may leave the producer suspended. Prefer an explicit resource boundary, such as a context manager around a file, rather than relying on eventual garbage collection as a cleanup policy.

The reading example has no file or network resource, so exhaustion is enough for this exercise. Do not infer resource guarantees from a simple in-memory demonstration.

Practise the explanation, not only the syntax

  • Point to the exact request that starts the generator body.
  • Describe what state exists after the first yield.
  • Show which inputs a second consumer could still read.
  • Explain whether a second pass needs a new source.

As a follow-up, write a one-pass count of high readings without turning them into a list. Then explain what would change if the caller also needed the values sorted. The answer should name both the traversal requirement and the extra storage.

References and next practice

docs.python.org — py model · docs.python.org — py data

Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud