Ad space (banner)
🐍Python Lessons
Lesson 13 / 69

Generators and yield

This lesson covers the basics of generators using yield, so you can understand how to handle large amounts of data memory-efficiently. It's written for anyone searching "Python yield generator" or "what does yield do in Python".

A function that uses yield is called a generator, and it returns values one at a time instead of all at once. Because it never needs to hold every value in memory simultaneously, it's great for working with large amounts of data. The idea is "produce a value only when it's needed" — a fundamentally different mechanism from an ordinary function.

The sample code's count_up_to function pauses execution every time it hits yield i, handing one value back to the caller. When you loop over a generator with a for statement, execution resumes right where it left off and runs until the next yield — a completely different flow of control than a normal return.

A common beginner mistake is not realizing a generator can only be consumed once. After you've pulled out every value, looping over the same generator again produces nothing. If you need to use it again, you have to call the generator function once more to create a fresh one.

Generators are a genuinely practical tool in real work — reading a huge log file one line at a time, for example, when memory is limited. Compared to a function that returns everything as a list, they're also more flexible when you want to stop processing partway through.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)