Python Itertools.accumulate() Function with Examples

Itertools Module:

The Python itertools module is a collection of tools for working with iterators.

Iterators are simply data types that can be used in a for loop. The list is the most commonly used iterator in Python.

Requirements for using itertools

Before using, the itertools module must be imported. Because we want to work with operators, we must also import the operator module.

The Itertools module is a collection of functions.

Itertools.accumulate() Function:

This iterator accepts two arguments:

the iterable target and the function to be followed at each iteration of value in target. If no function is specified, addition is performed by default. If the input iterable is empty, so will the output iterable.

This function generates an iterator that iterates through the results of a function.

Syntax:

itertools.accumulate(iterable[, func])

Parameter Values

iterable: This is Required. Elements to be gathered or accumulated

function: This is Optional. Iterables are accumulated using this function.

Examples:

Example1:

Input:

Given List = [4, 2, 1, 5, 3]

Output:

The cumulative of the given list = [4, 6, 7, 12, 15]

Example2:

Input:

Given List = [2, 3, 4, 1, 5]
Given function = lambda a, b: a*b

Output:

[2, 6, 24, 24, 120]

Itertools.accumulate() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the itertools.accumulate() method that calculates cumulative sum of the given list.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the cumulative of the given list.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the list as static input and store it in a variable.
gvn_lst = [4, 2, 1, 5, 3]
# Pass the given list as an argument to the itertools.accumulate() method that
# calculates cumulative sum of the given list.
# Store it in another variable.
rslt = itertools.accumulate(gvn_lst)
# Convert the above result into a list using the list() function and store
# it in a variable.
rslt_lst = list(rslt)
# Print the cumulative of the given list.
print("The cumulative of the given list =", rslt_lst)

Output:

The cumulative of the given list = [4, 6, 7, 12, 15]

with passing the iterable, function as parameters

import itertools
gvn_lst = [2, 3, 4, 1, 5]
rslt = itertools.accumulate(gvn_lst, lambda a, b: a*b)
print(list(rslt))

Output:

[2, 6, 24, 24, 120]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the itertools.accumulate() method that calculates cumulative sum of the given list.
  • Store it in another variable.
  • Convert the above result into a list using the list() function and store it in a variable.
  • Print the cumulative of the given list.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the itertools.accumulate() method that
# calculates cumulative sum of the given list.
# Store it in another variable.
rslt = itertools.accumulate(gvn_lst)
# Convert the above result into a list using the list() function and store
# it in a variable.
rslt_lst = list(rslt)
# Print the cumulative of the given list.
print("The cumulative of the given list =", rslt_lst)

Output:

Enter some random List Elements separated by spaces = 10 50 20 30 10
The cumulative of the given list = [10, 60, 80, 110, 120]