What does the "yield" keyword do in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
yield turns a function into a generator, which means the function can produce a sequence of values one at a time instead of building the whole result up front. Each time the generator yields a value, Python pauses the function and remembers its local state so execution can resume later.
yield pauses instead of finishing
A normal return sends back one value and ends the function. yield sends back one value and suspends the function.
The important behavior is that count is preserved between iterations. Python does not restart the function from the top after every value; it resumes right after the last yield.
Calling the function creates a generator object
This surprises many beginners. When you call a generator function, the body does not run to completion immediately. Instead, Python gives you a generator object.
next(gen) asks the generator for the next value. When no values remain, Python raises StopIteration.
That is why generators work naturally in for loops: the loop keeps calling next until the generator is exhausted.
Generators are lazy and memory-efficient
The biggest practical benefit of yield is lazy evaluation. If you need a million values but only consume the first ten, a generator avoids building a huge list that will mostly go unused.
This computes each square only when requested. That makes generators useful for:
- streaming large files
- processing database results incrementally
- representing infinite or very large sequences
- building data pipelines
It also means startup work can stay small, because the generator does not precompute the full result before iteration begins.
yield works well for infinite sequences
Because a generator only computes the next item on demand, it can represent an unbounded sequence safely.
This prints the first five even numbers without ever trying to store an infinite list.
yield is not just about memory
Generators are also a convenient way to express stateful iteration. They let you write code that looks like normal sequential logic instead of manually implementing a full iterator class with __iter__ and __next__.
That is why yield often makes parsing, traversal, and pipeline code easier to read. The function can keep local variables, loops, and conditionals naturally while still behaving like an iterator externally.
Common Pitfalls
- Expecting a generator function call to execute immediately instead of returning a generator object.
- Forgetting that generator values are consumed as you iterate, so you may need to recreate the generator to iterate again.
- Using
yieldwhen a simple list is clearer and the data size is small. - Confusing
yieldwithreturn;yieldpauses, whilereturnends the function. - Writing generator loops incorrectly and never updating the loop variable after yielding.
Summary
- '
yieldturns a function into a generator.' - A generator produces values one at a time and preserves its state between yields.
- Calling a generator function returns a generator object, not a finished result.
- Generators are useful for lazy evaluation, large data streams, and infinite sequences.
- '
yieldis often the cleanest way to write custom iteration logic in Python.'

