Open In App

Itertools.accumulate()-Python

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be empty. Example:

Python
import itertools

a = [1, 2, 3, 4]
print(list(itertools.accumulate(a)))

Output
[1, 3, 6, 10]

Explanation: accumulate() function iterates through the list, maintaining a running total. It starts with 1, adds 2 to get 3, adds 3 to get 6 and adds 4 to get 10, producing the cumulative sums.

Syntax of itertools.accumulate()

itertools.accumulate(iterable, func=None)

Parameters:

  • iterable: The input sequence (like a list, tuple, or set).
  • func (optional): A binary function (e.g., operator.add, operator.mul, max, etc.). If not provided, addition is used by default.

Returns: An iterator that yields accumulated results.

Examples of itertools.accumulate()

Example 1: In this example, we are using itertools.accumulate() along with operator.mul to compute the cumulative product of the elements in the list.

Python
import itertools
import operator

a = [1, 2, 3, 4, 5]
res = itertools.accumulate(a, operator.mul)
print(list(res))

Output
[1, 2, 6, 24, 120]

Explanation: Multiplication is applied from left to right. First, it starts with 1, then 1 * 2 = 2, then 2 * 3 = 6, followed by 6 * 4 = 24 and finally 24 * 5 = 120.

Example 2: In this example, we are using itertools.accumulate() with the built-in max function to compute the running maximum of the elements in the list.

Python
import itertools

a = [5, 3, 6, 2, 1, 9, 1]
res = itertools.accumulate(a, max)
print(list(res))

Output
[5, 5, 6, 6, 6, 9, 9]

Explanation: Maximum value is tracked as we move through the list. It starts with 5. Then max(5, 3) = 5, max(5, 6) = 6, max(6, 2) = 6, max(6, 1) = 6, max(6, 9) = 9 and max(9, 1) = 9.

Example 3: In this example, we are using itertools.accumulate() with a custom lambda function lambda x, y: x - y to compute a running subtraction.

Python
import itertools

a = [1, 2, 3, 4]
res = itertools.accumulate(a, lambda x, y: x - y)
print(list(res))

Output
[1, -1, -4, -8]

Explanation: Subtraction is applied from left to right using a custom lambda function. It starts with 1, then 1 - 2 = -1, then -1 - 3 = -4, and finally -4 - 4 = -8.

Example 4: In this example, itertools.accumulate() is used to compute the cumulative sum of the elements in the set difference b.difference(a).

Python
import itertools
a = {5, 3, 6, 2, 1, 9}
b = {4, 2, 6, 0, 7}

diff = b.difference(a) 
res = itertools.accumulate(diff)
print(list(res))

Output
[0, 4, 11]

Explanation: b.difference(a) gives the set of elements in b but not in a, which is {0, 4, 7} . Then addition is applied.Start with 0, then 0 + 4 = 4 and 4 + 7 = 11.


Next Article
Article Tags :
Practice Tags :

Similar Reads