Getting a series of accumulated values based on an iterable is a common requirement. With the help of the itertools.accumulate() function, we don’t need to write any loops to implement.
import itertools
import operator
nums = [1, 2, 3, 4, 5]
print(list(itertools.accumulate(nums, operator.mul)))
# [1, 2, 6, 24, 120]
The above program is the same as follows if we wouldn't like to use the operator.mul:
import itertools
nums = [1, 2, 3, 4, 5]
print(list(itertools.accumulate(nums, lambda a, b: a * b)))
# [1, 2, 6, 24, 120]