Python Yield Multiple Values
Last Updated :
29 Jan, 2024
In Python, yield is a keyword that plays a very crucial role in the creation of a generator. It is an efficient way to work with a sequence of values. It is a powerful and memory-efficient way of dealing with large values. Unlike a return statement which terminates the function after returning a value, yield produces and passes a series of values. In this article, we are going to deal with various scenarios where yield can be found a good fit. We will be dealing with some known questions and how can be they solved with the use of the yield keyword.
Python Yield Multiple Values
Below, we are going to explore various examples of Python Yield Multiple Values. We are going to see different problems with yield keywords.
Python Yield Multiple Values with Simple List Parsing
In this example, the below code defines a generator function `listIter` that yields each element of a given list. In the main function, a list `[1,2,3,4,5]` is defined, and the generator is created by calling `listIter(l)`. The `for` loop iterates through the generator, printing each element of the list on the same line.
Python3
def listIter(l):
for i in l:
yield i
if __name__ = = "__main__" :
l = [ 1 , 2 , 3 , 4 , 5 ]
gen = listIter(l)
for i in range ( len (l)):
print ( next (gen),end = " " )
|
Python Yield Multiple Values with Fibonacci Series
In this example, below code defines a generator function `fibo` to generate the Fibonacci series up to the nth term. It uses variables `a` and `b` as the first two elements, and `c` to calculate the next term. The generator yields each term one by one. In the main function, the generator is called with `n=20`, and the `for` loop prints the first 20 terms of the Fibonacci series.
Python3
def fibo(n):
a,b,c,i = 0 , 1 , 1 , 0
yield a
while i < n - 1 :
yield c
c = a + b
a = b
b = c
i + = 1
if __name__ = = "__main__" :
n = 20
gen = fibo(n)
for i in range (n):
print ( next (gen),end = " " )
|
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Python Yield Multiple Values with Finding Factors
In this example, below code defines a generator function `factors` to find and yield the factors of a given number `n`. In the main function, with `n=20`, the generator is called using `list(factors(n))`, and the factors of 20 are printed using a `for` loop.
Python3
def factors(n):
for i in range ( 1 , n + 1 ):
if n % i = = 0 :
yield i
if __name__ = = "__main__" :
n = 20
for i in list (factors(n)):
print (i,end = " " )
|
Python Yield Multiple Values with Random Number Generator
In this example, below code utilizes the `random` module to generate a series of random numbers between 0 and 10 using the `randomNum` generator function. In the main function, with `n=5`, the generator is called using `list(randomNum(n))`, and a `for` loop prints five random numbers on a single line.
Python3
import random
def randomNum(n):
for i in range (n):
yield round (random.random() * 10 )
if __name__ = = "__main__" :
n = 5
for i in list (randomNum(n)):
print (i,end = " " )
|
Conclusion
In Python, yield keyword in used in the creation of a generator. Unlike returns, it do not terminates a method rather it temporarily suspends the method. We can pass more than one value from a method to the main function or another method with the help of yield keyword. In this article we have covered use cases of yield keyword with some working examples along with their explanations.
Share your thoughts in the comments
Please Login to comment...