The yield Keyword in Python
This tutorial will explain the purpose and use of the yield
keyword in Python. The yield
keyword is a Python statement used to define the generator functions in Python. The yield
statement can only be used within the body of the function.
The main difference between a generator function and a regular function is that the generator function contains a yield
expression instead of the return
statement. The yield
statement produces the series of values called the generator iterator as output. New values from the iterator can be retrieved using the next()
function or the for
loop.
Each time the next()
function is called or at each iteration of the for
loop, the yield
statement yields or produces a new value and saves the location execution state of the function i.e. the values of the local variables, etc. On each new next()
function call or iteration, the yield
statement resumes from the last saved state, unlike the return
statement that starts at each call.
Example Codes of the yield
Keyword in Python
As the working of the yield
statement is clear, let us now look into its uses and example codes. Suppose we have a large amount of data and it can not be loaded at once into an iterable object, or we want a more memory-efficient way to read the data. We can create a generator function using the yield statement; the function will read and yield the new chunk of data at each iteration or next()
function call.
The below example code demonstrates how to use the yield
statement with the for
loop to create a simple generator function in Python. The generator function my_generator()
in the below example code will yield the new number from 1
to 100
and work till 99
calls.
def my_generator():
for x in range(1, 100):
print("new value is generated!")
yield x
We will first need a generator iterator object to generate values by using either the next()
function or through iteration.
gen_iter = my_generator()
Now let’s get the new values from the generator iterator using the next()
function; we can also use the next()
function as iterator.__next__()
, as shown in the below example code.
val = next(gen_iter)
print("Value =", val)
val = gen_iter.__next__()
print("Value =", val)
Output:
new value is generated!
Value = 1
new value is generated!
Value = 2
The other method to get new values is to iterate through the generator iterator object using the for
loop; the below example code demonstrates how to iterate through the generator iterator object in Python.
for val in gen_iter:
print("Value =", val)
The above code example will generate all the 99
values from the specified range in the generator function.
We can also create a generator function without the for
loop by using the yield
statement multiple times, as demonstrated in the below example code.
def my_generator():
x = 1
print("new value is generated!")
yield x
x = x + 1
print("new value is generated!")
yield x
x = x + 1
print("new value is generated!")
yield x
Now, let’s use our new generator function to generate all the values using the for
loop.
gen_iter = my_generator()
for val in gen_iter:
print("Value =", val)
Output:
new value is generated!
Value = 1
new value is generated!
Value = 2
new value is generated!
Value = 3