What is ‘yield’ keyword in Python and what does it do?

What is ‘yield’ keyword in Python and what does it do?

Answer: The “yield” keyword in Python is used to turn a function into a generator.

By using the ‘yield’ keyword, the generator returns multiple values, one at a time, instead of returning all the values at once. It also stops the execution of the function once all the values are returned. In this blog let’s explore the multiple ways how the ‘yield’ keyword in Python can be used along with its functionalities.

Table of Contents:

Features of the ‘yield’ keyword

  • Pause and resume: The ‘yield’ keyword allows you to pause the function and resume it later. And allowing you to produce multiple values one at a time.
  • Memory efficient: This keyword generates values without storing them all in the memory which makes it more memory efficient.
  • Iterable: Function when used with the ‘yield’ keyword returns the generator that you can loop through easily.
  • State saving: It remembers the function state between the pauses, so you can continue from where you left off.
  • Multiple values: Allows you to get a sequence of numbers over time instead of getting them all at once.

Advantages of the ‘yield’ keyword

  • Producing the items when needed, can make the program more efficient and responsive.
  • Values are computed as per the requirement, which can save resources and improve performance in scenarios where all generated values are not required.
  • Generators with `yield` make code easier to read and shorter than traditional methods.

Usage of ‘yield’ Keyword in Python

Now, we’ll discuss about some of the usages of the ‘yield’ keyword in Python:

Usage 1: Generator Function and ‘yield’ Keyword in Python

This shows how a generator function works and how the yield keyword is used to return values from the function one at a time.

Example 1:

Python

Output:

Example 2:

The display_status() function is a generator. It uses yield to pause and return control to the caller. Here we can resume the function where it has stopped.

Python

The program prints the next status only when the next() function is called.

Output:

Usage 2: Using the “yield” Keyword as a Boolean

You can use the ‘yield’ keyword along with Boolean logic (like if statements) to control what values the generator function returns. Depending on the condition, it can yield different values or pause at different points.

Example 1:

Python

Output:

Usage 3: To Generate an Infinite Sequence using the ‘yield’ Keyword

The infinite sequence of numbers is generated using the ‘yield’ keyword, which returns the number by incrementing it with +1. Using the ‘yield’ keyword is better than traditional looping because it is memory efficient as it stores only the last generated value.

Example 1:

Python

Output:

Usage 4: Using the ‘yield’ Keyword in the List

The ‘yield’ keyword is used to go through a list and extract only the odd numbers. Instead of returning all the numbers at once, it yields each odd number one by one.

Example 1:

Python

Output: 

Difference Between the ‘yield’ and ‘return’ Keywords in Python

Features            yield keyword              return keyword
PurposePauses the function and return a generatorEnd the function and return the value
StateSaves the function stateDoes not save the state
IterationProduces a sequence of valueProduces a single-value
Memory usageMore memory efficientUses more memory when returning  large data
Use caseUseful for large data setsUseful for single results or small data sets

Conclusion

The “yield” keyword in Python is used to create generator functions, allowing efficient memory use, and can generate values one at a time. Understanding the examples discussed above, helps you use the ‘yield’ keyword in an effective way in Python.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner