Yield in Python Tutorial

Yield in Python Tutorial: Generator & Yield vs Return Example

What is Python yield?

The yield keyword in python works like a return with the only

difference is that instead of returning a value, it gives back a generator object to the caller.

When a function is called and the thread of execution finds a yield keyword in the function, the function execution stops at that line itself and it returns a generator object back to the caller.

Syntax

yield expression

Description

Python yield returns a generator object. Generators are special functions that have to be iterated to get the values.

The yield keyword converts the expression given into a generator function that gives back a generator object. To get the values of the object, it has to be iterated to read the values given to the yield.

Example: Yield Method

Here is a simple example of yield. The function testyield() has a yield keyword with the string “Welcome to GGI Python Tutorials”. When the function is called, the output is printed and it gives a generator object instead of the actual value.

Syntax

yield expression

Description

Python yield returns a generator object. Generators are special functions that have to be iterated to get the values.

The yield keyword converts the expression given into a generator function that gives back a generator object. To get the values of the object, it has to be iterated to read the values given to the yield.

Example: Yield Method

Here is a simple example of yield. The function testyield() has a yield keyword with the string “Welcome to GGI Python Tutorials”. When the function is called, the output is printed and it gives a generator object instead of the actual value.

Generators are one-time Use

Incase of generators they are available for use only once. If you try to use them again, it will be empty.

For example:

def even_numbers(n):
for x in range(n):
if (x%2==0):
yield x
num = even_numbers(10)
for i in num:
print(i)

print("\n")
print("Calling the generator again: ", list(num))

Output:

0
2
4
6
8
Calling the generator again: []

In case you want the output to be used again, you will have to make the call to function again.

Example: Generators and yield for Fibonacci Series

The following example shows how to use generators and yield in Python. The example will generate the Fibonacci series.

def getFibonnaciSeries(num):
c1, c2 = 0, 1
count = 0
while count < num:
yield c1
c3 = c1 + c2
c1 = c2
c2 = c3
count += 1
fin = getFibonnaciSeries(7)
print(fin)
for i in fin:
print(i)

Output:

<generator object getFibonnaciSeries at 0x0000007F39C8BA20>
0
1
1
2
3
5
8

Example: Calling Function with Yield

In this example will see how to call a function with yield.

The below example has a function called test() that returns the square of the given number. There is another function called getSquare() that uses test() with yield keyword. The output gives the square value for given number range.

def test(n):
return n*n

def getSquare(n):
for i in range(n):
yield test(i)

sq = getSquare(10)
for i in sq:
print(i)

Output:

0
1
4
9
16
25
36
49
64
81

When to use Yield Instead of Return in Python

Python3 Yield keyword returns a generator to the caller and the execution of the code starts only when the generator is iterated.

return in a function is the end of the function execution, and a single value is given back to the caller.

Here, is the situation when you should use Yield instead of Return

  • Use yield instead of return when the data size is large
  • Yield is the best choice when you need your execution to be faster on large data sets
  • Use yield when you want to return a big set of values to the calling function
  • Yield is an efficient way of producing data that is big or infinite.

Yield vs. Return

Here, are the differences between Yield and Return

YieldReturn
Yield returns a generator object to the caller, and the execution of the code starts only when the generator is iterated.A return in a function is the end of the function execution, and a single value is given back to the caller.
When the function is called and it encounters the yield keyword, the function execution stops. It returns generator object back to the caller. The function execution will start only when the generator object is executed.When the function is called, the execution starts and the value is given back to the caller if there is return keyword. The return inside the function marks the end of the function execution.
yield expressionreturn expression
No memory is used when the yield keyword is used.The memory is allocated for the value returned.
Very useful if you have to deal with huge data size as the memory is not used.Convenient for very small data size.
The performance is better if the yield keyword is used for large data size.A lot of memory is used if the data size is huge that will hamper the performance.
Execution time is faster in case of yield for large data size.The execution time used is more as there is extra processing done in case if your data size is huge, it will work fine for small data size.

Summary:

  • The yield keyword in python works like a return with the only difference is that instead of returning a value, it gives back a generator function to the caller.
  • A generator is a special type of iterator that, once used, will not be available again. The values are not stored in memory and are only available when called.
  • The values from the generator can be read using for-in, list() and next() method.
  • The main difference between yield and return is that yield returns back a generator function to the caller and return gives a single value to the caller.
  • Yield does not store any of the values in memory, and the advantage is that it is helpful when the data size is big, as none of the values are stored in memory.
  • The performance is better if the yield keyword is used in comparison to return for large data size.
Design a site like this with WordPress.com
Get started