Open In App

Python Yield Multiple Values

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




#generator Function
def listIter(l):
    for i in l:
        yield i
 
#main function
if __name__ == "__main__":
      #defining a list
    l= [1,2,3,4,5]
     
    #function calling
    gen = listIter(l)
     
    for i in range(len(l)):
        print(next(gen),end= " ")


Output

1 2 3 4 5 

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




#fibonacci function
def fibo(n):
   # a,b are first two elements of fibonacci series
    a,b,c,i = 0,1,1,0
     
    #passing the first element to the main function
    yield a
     
    while i < n-1:
        yield c
        c = a+b
        a=b
        b=c
        i += 1
 
#Main Function
if __name__ == "__main__":
    n=20
    #Function Calling
    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




#factors function
def factors(n):
    #finding factors
    for i in range(1, n + 1):
        if n % i == 0:
            yield i
 
#main method
if __name__ == "__main__":
    n=20
     
    #function calling and displaying
    for i in list(factors(n)):
        print(i,end=" ")


Output

1 2 4 5 10 20 

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




#importing random module
import random
 
#defining the generator function
def randomNum(n):
    for i in range(n):
        yield round(random.random()*10)
 
#main function
if __name__ == "__main__":
    n=5
     
    #calling the function
    for i in list(randomNum(n)):
        print(i,end=" ")


Output

1 4 4 6 2 

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads