Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 95)
95.
What is the purpose of the itertools.chain() function when used with generators?
It concatenates multiple generators
It applies a specified function cumulatively to the elements of a generator
It interleaves values from different generators
It yields elements from multiple generators in sequence
Answer: Option
Explanation:
itertools.chain() is used to chain multiple iterables (including generators) together, producing a single iterable that yields elements from each iterable in sequence.
import itertools

gen1 = (1, 2, 3)
gen2 = ('a', 'b')

chained_iterable = itertools.chain(gen1, gen2)

print(list(chained_iterable))  # Output: [1, 2, 3, 'a', 'b']
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.