Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 77 ยท Python Interview Questions

Python: Iterators, Generators and Comprehensions

Explain the iterator protocol, yield, lazy evaluation and comprehension pitfalls in Python interviews, with runnable examples and clear short answers.

"What is the difference between an iterable and an iterator?" and "What does yield do?" are two of the most common Python interview questions. They test whether you understand what a for loop really does, and whether you can process large or endless data without loading it all into memory.

You can read the full lessons at iterators, generators and list comprehensions. This lesson gives the interview answers. If the previous lesson is fresh in your mind, remember that Python's data model is what makes __iter__ and __next__ work with the for statement.

Iterable versus iterator

An iterable is any object you can loop over. It has __iter__(), which returns an iterator. An iterator is the object that does the work: it has __next__(), which returns the next item or raises StopIteration, and its own __iter__() returns itself. Lists, strings, dicts and files are iterables; iter(my_list) gives an iterator.

What a for loop does

Call iter() Get an iterator from the iterable.
Call next() Take one item at a time.
StopIteration The loop ends quietly.

Iterators are one-shot. Iterables can be looped again.

1 / 2
Python The protocol by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A common follow-up: why is it useful that Countdown is an iterable and not an iterator? Because you can loop over it as many times as you like. An iterator can be consumed only once.

Generators and yield

A generator function contains yield. Calling it does not run the body; it returns a generator object (an iterator). Each next() runs the body until the next yield, returns that value and freezes the function's state (local variables and position) until the next call.

Python State kept between yields

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Mention these when asked: return inside a generator ends it (the value becomes part of StopIteration), .send() lets you push values in, and yield from other delegates to another iterable.

Lazy evaluation and memory

A list holds all items at once. A generator produces them on demand and holds only its current state. Use generators for big files, streams and pipelines; use lists when you need random access, len() or several passes.

Python List versus generator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pipeline output shows items flowing one at a time through both stages: "produce 1, double 1" then the result, with no work done in advance. Short-circuiting functions such as any(), all() and next() stop pulling as soon as the answer is known.

Comprehensions

A comprehension builds a list, set, dict or generator from an iterable in one expression: [expr for x in items if cond]. They are faster and clearer than a loop with append for simple transformations.

Python Comprehension forms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pitfalls interviewers like

Generator exhausted. A generator can be iterated once. Looping over it again yields nothing, with no error.

Late binding in comprehensions of lambdas. The loop variable is looked up when the lambda runs, not when it is created.

Side effects in comprehensions. Writing [print(x) for x in items] builds a throwaway list; use a plain loop.

Comprehension scope. The loop variable of a comprehension is local to the comprehension and does not leak, unlike a for statement.

Python Exhaustion, late binding and scope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What distinguishes an iterator from an iterable?

    An iterable gives you an iterator through iter. The iterator keeps position and provides next, and is used up after one pass.
  2. 2. What happens when you call a generator function?

    The body runs only when the generator is advanced with next() or a for loop.
  3. 3. What does sum(g) + sum(g) give when g is a generator expression?

    The first sum consumes the generator. The second one sees no items and returns 0.
  4. 4. Which is a good reason to prefer a generator over a list?

    Generators produce values lazily and use little memory. Indexing, length and repeated passes need a real list.

Summary

  • A for loop calls iter() on an iterable and next() on the iterator until StopIteration.
  • Iterators are single-use; an iterable can create fresh iterators.
  • A function with yield returns a generator that pauses and resumes with its state intact.
  • Generators are lazy and memory-friendly; lists give random access and repeat passes.
  • Watch for exhausted generators, late-binding lambdas and misplaced conditionals in comprehensions.

Next: Python: Decorators, Closures and Context Managers.