Skip to content
UniCircle
MediumConceptualIterators and generators

Generators and yield: how are they different from returning a list?

Question PY-GEN-005

What is a generator in Python? Compare def squares(n): return [i*i for i in range(n)] with a version that uses yield. When does the generator version matter, and what is the iterator protocol?

Show the explanation

A generator function contains yield. Calling it does not run the body. It returns a generator object. Each next() resumes the function until the next yield, hands that value back, and pauses again, keeping all local variables. When the function returns, the generator raises StopIteration and a for loop ends.

Python
def squares(n):
    for i in range(n):
        yield i * i

g = squares(3)
next(g), next(g), next(g)   # (0, 1, 4); a fourth next() raises StopIteration

List vs generator

return [ ... ]yield
When values are computedall at once, up frontlazily, one per next()
MemoryO(n)O(1)
Can you iterate twice?yesno: once it is used up, it is empty
len() and indexingyesno

When it matters

  • Huge or infinite sequences, such as reading a 10 GB log line by line (for line in open(path) is itself lazy) or itertools.count().
  • Pipelines that never build intermediate lists: sum(x * x for x in data if x > 0). That is a generator expression, written with parentheses.
  • Stopping early. any(...) stops at the first True, so the remaining values are never computed.

The iterator protocol. An iterable has __iter__(), which returns an iterator. An iterator has __next__(), which returns the next value or raises StopIteration. for x in obj calls iter(obj) and then next() repeatedly. Generators implement both methods automatically, which is why they are the easiest way to write a custom iterator.

Extras worth naming: yield from sub_gen delegates to another generator. itertools (chain, islice, groupby) works lazily on iterators. Generators also have send(), the basis of early coroutines, though modern async code uses async def and await.

Original UniCircle question. Last reviewed 22 Oct 2026.