Using Yield Keyword in Python

The yield is a Python keyword that returns a value from a function while the function retains its local variables between calls. When called again, such a function is executed from the point in the code where it was interrupted by the yield statement. Yield in Python is similar to the "return" statement used to return values ​​or objects from function. However, there is a slight difference. Any function that contains the yield keyword is called a generator. The yield keyword converts the expression provided with it into a generator object and returns it to its caller. Therefore, if you want to get the values ​​stored inside the generator object, you need to iterate over it. The yield keyword in Python is less well known but holds promise. Click Execute to run Python Yield Example online and see the result.
Using Yield Keyword in Python Execute
def count():
    i = 1
    while True:
        yield i
        i += 1
for value in count():
    if value > 5:
Updated: Viewed: 1731 times

What is Python?

The Python programming language is one of the most popular general-purpose programming languages, used for server-side programming, machine learning, application testing, and as "glue code" to connect various components. An interpreted and object-oriented language, Python has dynamic typing and dynamic binding, as well as an integrated garbage collector. The Python programming language runs on almost all operating systems, such as Windows, Linux, and macOS. Python is easy to learn, and code written in Python is easy to maintain. Python has built-in modules for working with JSON, strings, HTTP requests, and XML out of the box.

What is yield?

In Python, yield is a keyword that can be used as a function return statement. In the yield case, the function does not return output but rather a generator object that can be iterated over. Each time you iterate, Python runs the code until it encounters a yield statement inside the function. It then sends the computed values and suspends the function in that state without exiting. The next time the function is called, it restores the state in which it was last suspended, and execution continues from there. Until the generator is exhausted (calling yield operator), this process continues.

Python Yield Function Example
my_list = [1, 2, 3, 8, 15, 42]

def get_even(my_list):
    for i in my_list:
        if i % 2 == 0:
            yield i

print ("Only even numbers: ", end = "")
for i in get_even(my_list):
    print (i, end = " ")

# Only even numbers: 2 8 42 

What are the benefits of Python yield?

Following are the benefits of yield in Python:

  • Reduces overall execution time. At the next call, the generator resumes its work and does not start from the very beginning
  • It is not necessary for the developer to deal with the overhead of allocating and freeing memory. Because generators automatically store and manage the states of their local variables

What are the disadvantages of Python yield?

Following are the disadvantages of yield in Python:

  • Sometimes it isn't easy to understand the logic behind the code immediately. You have to pay for the optimization of the running time and use memory with the complexity of the code
  • Using yield can cause errors, primarily if the function call is not handled correctly

What is the difference between yield and return in Python?

When using the return statement, all values of local variables will be lost after exiting the function. The next time the same function is called, the function execution will start from the beginning. Conversely, when using yield, when the function exits, it will remember its “state”, including all the values of local variables, and continue from where it left off.

See also