All concepts

Generators

yield values lazily, one at a time—stream huge data in constant memory.

Python · Intermediate · ~5 min

In plain English

A function that hands back one item at a time and pauses in between, instead of building the whole list in memory first.

Why it's worth your time

It's how you process a file bigger than your RAM, and the mental model behind every streaming API you'll write.

If you remember three things

  • yield pauses and resumes; the function keeps its state
  • Lazy: nothing computes until you iterate
  • Single-use — once consumed, it's exhausted

Overview

A generator function contains yield; calling it returns a lazy generator object that runs no code until iterated. Each yield emits one value and suspends, freezing local state so it resumes exactly where it left off. This streams data in constant memory instead of building a full list.

In an interview

A generator produces values lazily using yield: each call to next() runs the function until the next yield, hands back one value, and pauses with all local state intact. That means constant memory—you never materialize the whole sequence—so it's ideal for streaming large files or infinite sequences. A generator expression is the same thing inline, using parentheses instead of a list's brackets.

Production defaults

Large data
generator over list, always. Memory stays flat regardless of size
Chaining
generators compose into pipelines without materializing intermediates
Careful
you cannot len() one, and you cannot iterate it twice. Materialize if you need either

What breaks

  • Second loop over it does nothing — Generators are exhausted after one pass. Wrap in list() if you need to reuse, and accept the memory cost.
  • Exceptions surface far from the cause — Lazy evaluation means the error fires where you consume, not where you defined it. Read the full traceback.

Watch it explained

Generators in Python || Python Tutorial || Learn Python Programming — Socratica, 8:31

Related