Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 90)
90.
What is the purpose of the itertools.accumulate() function when used with generators?
It concatenates multiple generators
It applies a specified function cumulatively to the elements of a generator
It stops the generator after one iteration
It interleaves values from different generators
Answer: Option
Explanation:
itertools.accumulate() applies a specified function cumulatively to the elements of a generator, yielding the intermediate results.
import itertools

gen = (1, 2, 3, 4)
accumulated_iterable = itertools.accumulate(gen, lambda x, y: x + y)

print(list(accumulated_iterable))  # Output: [1, 3, 6, 10]
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.