Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 58)
58.
What is the purpose of the itertools.cycle() function when used with generators?
It creates an infinite sequence of repeated values from the generator
It generates a single iteration from the generator
It stops the generator after one complete cycle
It shuffles the values produced by the generator
Answer: Option
Explanation:
itertools.cycle() creates an infinite sequence of repeated values. When used with a generator, it continuously cycles through the values produced by the generator.
import itertools

def my_generator():
    yield 1
    yield 2
    yield 3

cycled_generator = itertools.cycle(my_generator())

for _ in range(10):
    print(next(cycled_generator))
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.