A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-itertools-accumulate/ below:

Itertools.accumulate()-Python - GeeksforGeeks

Itertools.accumulate()-Python

Last Updated : 02 May, 2025

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)))

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.

itertools.accumulate(iterable, func=None)

Parameters:

Returns: An iterator that yields accumulated results.

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))

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))

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4