Module 10 · Comprehensions and Advanced Iteration — topic 12 of 13
yield
The yield statement in detail: yielding values from loops, multiple yields, yield from for delegation, generator return values, and the difference between yield and return.
Learning objectives
- Use yield inside loops and conditions
- Delegate to sub-generators with yield from
- Contrast yield with return
Definition
yield is a keyword used inside a function to produce a value to the caller and suspend the function's execution, which resumes after the yield on the next request. yield from iterable yields every value of another iterable.
Introduction
return hands back one value and ends the function. yield hands back a value and pauses, ready to continue. A generator can yield as many times as it likes.
Why it matters
yield from makes it easy to compose generators and to walk recursive structures such as folder trees or nested lists lazily.
Detailed explanation
A generator can contain several yield statements; they produce values in the order they are reached.
yield inside a loop produces one value per iteration — the most common pattern.
yield from iterable is shorthand for: for item in iterable: yield item. It's ideal for delegating to another generator, including the same function recursively.
return inside a generator ends it (raising StopIteration). A return value becomes the value of a yield from expression in the caller, which is rarely needed in everyday code.
Because the function's state is kept between yields, generators naturally express processes with steps, such as tokenisers or state machines.
Syntax
- Each yield produces one value.
- yield from produces all values of an iterable.
- return ends the generator.
Code examples
Several yields and yield from
[1, 2, 3, 4, 5, 6]
yield versus return
a ['a', 'b', 'c']
More examples
Recursive generators
[1, 2, 3, 4, 5, 6, 7, 8] src/main.py 120 src/utils/io.py 40 src/utils/math.py 75 README.md 12 total 247
Step-by-step explanation
- flatten yields plain items and delegates nested lists or tuples to a recursive call with yield from.
- files walks a dictionary that models a folder tree, building each file's path as it goes.
- Because files is a generator, sum can total the sizes without building a list.
- Calling files(tree) again starts a fresh walk.
Expected output
Running “Recursive generators” prints exactly:
Practical applications
- Walking directory trees (os.walk is itself a generator).
- Flattening nested JSON for export to CSV.
Best practices
- Use yield from to delegate instead of an explicit inner loop.
- Write recursive generators for tree-shaped data.
- Keep generator functions focused on producing values.
Common mistakes
Forgetting yield from in recursion
Avoid
Prefer
Calling a generator function returns a generator; you must yield its values.
Yielding a list instead of its items
Avoid
Prefer
yield produces the whole list as one value; yield from produces each element.
Important notes
- Generators also support send() and close(), used for coroutines; modern async code uses async/await instead.
Key takeaways
- yield produces a value and pauses; return ends the generator.
- A generator can yield many times.
- yield from delegates to another iterable or generator.
Try it yourself
- 1Write a generator that yields every key path in a nested dictionary, like 'a.b.c'.
- 2Write chain(*iterables) using yield from.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?