What is “yield” keyword in Python? – Complete Guide

In this python tutorial, you will learn about yield keyword. Like, what does yield keyword do in Python and why do we need it?

Agenda

Let’s dive into the tutorial.

What is yield?

In python, the yield keyword is used within a function to return the values without destroying the local variables and without stoping the flow of function. We can call the function a generator, if the function uses the yield keyword.

Usage of the return keyword

A return statement is used to return a value from a function and also to exit from the function. But what if we want to return multiple values from a function and that too at the different time, then it is not possible by the return keyword.

Advertisements

Syntax:

def function():
    return value

print(function())

Let’s see the example to understand it better. In this example, we will return the even numbers from the list through a function call

# generator to print even numbers
def return_even(input_list) :
    #use for loop iterate elements from the list
    for iterator in input_list:
        #condition to check if the element is even
        if (iterator % 2 == 0):
            return iterator

# list of 10 integers 
input_list = [1, 2,3,4,5,6,7,8,9,19]

# display actual list
print(input_list)

# call the function that displays even numbers
print (return_even(input_list))

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 19]
2

So from the above output, you can see that only 2 is returned as the even number, then what about 4,6, and 8? As we said, that the return keyword will stop executing the remaining program. In that case, using yield is the best idea to iterate and check all the elements rather than returning the first value only.

That’s why the yield keyword is important.

Usage of the “yield” keyword

Syntax:

def function():
    yield value

print(function())

Let’s see the example to understand it better. In this example, we will return even numbers from the list through a function call,

# generator to print even numbers
def return_even(input_list) :
    #use for loop iterate elements from the list
    for iterator in input_list:
        #condition to check if the element is even
        if (iterator % 2 == 0):
            yield iterator

# list of 10 integers 
input_list = [1, 2,3,4,5,6,7,8,9,19]

# display actual list
print(input_list)

# call the function that displays all even numbers
for iterator in return_even(input_list):
    print (iterator)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 19]
2
4
6
8

So from the above output, you can see that all the even numbers are returned.

Using yield with list data structure

Let’s demonstrate an example in which we can implement the usage of list data structure with yield keyword.

Example:

In this example, we will create a list of integers and return odd and even values separately.

# generator to print even numbers
def return_even(input_list) :
    #use for loop iterate elements from the list
    for iterator in input_list:

    #condition to check if the element is even
        if (iterator % 2 == 0):
            yield iterator


# generator to print odd numbers
def return_odd(input_list) :
    #use for loop iterate elements from the list
    for iterator in input_list:
        #condition to check if the element is odd
        if (iterator % 2 != 0):
           yield iterator

# list of 10 integers 
input_list = [1, 2,3,4,5,6,7,8,9,19]

# display actual list
print(input_list)

# call the function that displays all even numbers
for iterator in return_even(input_list):
    print (iterator, end=" ")

print()

# call the function that displays all odd numbers
for iterator in return_odd(input_list):
    print (iterator, end=" ")

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 19]
2 4 6 8 
1 3 5 7 9 19 

Using yield with tuple data structure

Let’s demonstrate an example in which we can implement the usage of tuple data structure with yield keyword.

Example:

In this example, we will create a tuple of integers and return odd and even values separately.

# Generator to print even numbers
def return_even(input_tuple) :
    #use for loop iterate elements from the list
    for iterator in input_tuple:
        #condition to check if the element is even
        if (iterator % 2 == 0):
            yield iterator


# generator to print odd numbers
def return_odd(input_tuple) :
    #use for loop iterate elements from the list
    for iterator in input_tuple:
        #condition to check if the element is odd
        if (iterator % 2 != 0):
            yield iterator

# tuple of 10 integers 
input_tuple = (1, 2,3,4,5,6,7,8,9,19)

# display actual tuple
print(input_tuple)

# call the function that displays all even numbers
for iterator in return_even(input_tuple):
    print (iterator,end=" ")

print()

# call the function that displays all odd numbers
for iterator in return_odd(input_tuple):
    print (iterator,end=" ")

Output:

(1, 2, 3, 4, 5, 6, 7, 8, 9, 19)
2 4 6 8 
1 3 5 7 9 19

Advantages of the yield keyword

  • Controlling Memory Allocation: It will store the states of the local variable, so, memory is controlled.
  • Time Saving: In yield, the old state is retained and the flow will not start from the beginning. Therefore the time is saved.

Summary

In this tutorial, we have seen what is a yield keyword in Python and how to use it rather than the return statement. So we can use yield because it saves time and it is memory efficient.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top