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:
- 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.
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))
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.
Similar Reads
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
12 min read
Python - Itertools.count()
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
3 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
3 min read
Python - itertools.repeat()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Itertools.accumulate()-Python
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 emp
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
4 min read
Python - Itertools.chain.from_iterable()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th
2 min read
Python - Itertools.compress()
Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform
1 min read
Python - Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
2 min read