Python Intermediate: How to use Yield in Python

Home /

Table of Contents

Yield in Python

In Python, yield is a powerful keyword that is used in defining generator functions. A generator function is a special type of function that returns an iterator object which can be iterated over using the next() method. When the yield keyword is used inside a generator function, it allows the function to produce a series of values that can be retrieved one at a time by the iterator object.

The yield keyword is used to “pause” the generator function and save its current state, including the current values of local variables. The next time the generator is called, it will resume execution from where it left off, using the saved state to continue producing values.

One of the key benefits of using yield is that it allows us to generate values on the fly without having to store them all in memory at once. This can be especially useful when dealing with large amounts of data, where storing everything in memory at once would be impractical or even impossible.

Another benefit of using yield is that it allows us to create generators that are more flexible than simple lists or arrays. For example, we can use a generator to produce an infinite sequence of values or to generate values based on some external input or condition.

Yield instead of Return

The yield statement pauses a function’s execution, returns a value to the caller, and saves enough state for the function to continue from where it left off. When the function is resumed, it continues execution immediately after the last yield run. This enables its code to generate a sequence of values over time, rather than computing them all at once and returning them as a list.


def produce_function():
   yield 'A'
   yield 'B'
   yield 'C'
for items in produce_function():



While Yield can yield a series of results, Return sends a specific value to its caller. When we want to iterate over a series but don’t want to store the complete sequence in memory, we should use yield.

Yield from Generators

Python generators make use of yield. A generator function is defined similarly to a regular function, but if it needs to generate a value, it uses the yield keyword rather than the return keyword. If yield appears in the body of a def, the function is automatically converted to a generator function.


def qubic_function():
   initial = 1
   while True:
       yield initial**3           
       initial += 1
for items in qubic_function():
   if items > 200:



The primary advantage of using yield is that it allows you to generate a series of values without needing to create a list or other collection to hold them all. This can save memory and improve performance, particularly when working with large data sets.

Another advantage of using yield is that it allows you to create infinite sequences, which can be useful in certain cases.

The yield keyword is an essential feature of Python that allows functions to return generators. Generators are functions that generate a sequence of values, one at a time, rather than returning a single value. The use of generators can lead to more efficient and readable code, especially when dealing with large datasets.

One of the main advantages of using yield in Python is that it allows for the lazy evaluation of data. Instead of generating all the data at once, generators create an iterable that generates values on-the-fly as they are needed. This can save memory and improve performance, especially when dealing with large datasets.

Additionally, using yield can make code more readable and concise. It allows for the creation of generators that can be used with loops, comprehensions, and other iterable constructs, making it easy to iterate over complex data structures or perform complex operations on large datasets.

Share The Tutorial With Your Friends

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article