Open In App

Python – Itertools.accumulate()

Last Updated : 31 Mar, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Python itertools module is a collection of tools for handling iterators.

According to the official documentation:

“Module [that] implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML… Together, they form an ‘iterator algebra’ making it possible to construct specialized tools succinctly and efficiently in pure Python.” this basically means that the functions in itertools “operate” on iterators to produce more complex iterators.

Simply put, iterators are data types that can be used in a for loop. The most common iterator in Python is the list.

Let’s create a list of strings and named it colors. We can use a for loop to iterate the list like:




colors = ['red', 'orange', 'yellow', 'green']
  
# Iterating List
for each in colors:
    print(each)


Output:

red
orange
yellow
green

There are many different kinds of iterables but for now, we will be using lists and sets.

Requirements to work with itertools

Must import the itertools module before using. We have to also import the operator module because we want to work with operators.

import itertools
import operator ## only needed if want to play with operators

Itertools module is a collection of functions. We are going to explore one of these accumulate() function.

Note: For more information, refer to Python Itertools

accumulate()

This iterator takes two arguments, iterable target and the function which would be followed at each iteration of value in target. If no function is passed, addition takes place by default. If the input iterable is empty, the output iterable will also be empty.

Syntax
itertools.accumulate(iterable[, func]) –> accumulate object

This function makes an iterator that returns the results of a function.

Parameters
iterable & function

Now its enough of the theory portion lets play with the code

Code: 1




# import the itertool module 
# to work with it
import itertools
  
# import operator to work 
# with operator
import operator
  
# creating a list GFG
GFG = [1, 2, 3, 4, 5]
  
# using the itertools.accumulate() 
result = itertools.accumulate(GFG, 
                              operator.mul)
  
# printing each item from list
for each in result:
    print(each)


Output:

1
2
6
24
120

Explanation :

The operator.mul takes two numbers and multiplies them.

operator.mul(1, 2)
2
operator.mul(2, 3)
6
operator.mul(6, 4)
24
operator.mul(24, 5)
120

Now in the next example, we will use the max function as it takes a function as a parameter also.

Code 2:




# import the itertool module 
# to work with it
import itertools
  
# import operator to work with
# operator
import operator
  
# creating a list GFG
GFG = [5, 3, 6, 2, 1, 9, 1]
  
# using the itertools.accumulate()
  
# Now here no need to import operator
# as we are not using any operator
# Try after removing it gives same result
result = itertools.accumulate(GFG, max)
  
# printing each item from list
for each in result:
    print(each)


Output:

5
5
6
6
6
9
9

Explanation:

5
max(5, 3)
5
max(5, 6)
6
max(6, 2)
6
max(6, 1)
6
max(6, 9)
9
max(9, 1)
9

Note: The passing function is optional as if you will not pass any function items will be summed i.e. added by default.

itertools.accumulate(set.difference)
This return accumulate of items of difference between sets.

Code to explain




# import the itertool module to
# work with it
import itertools
  
  
# creating a set GFG1 and GFG2
GFG1 = { 5, 3, 6, 2, 1, 9 }
GFG2 ={ 4, 2, 6, 0, 7 }
  
# using the itertools.accumulate()
  
# Now this will first give difference 
# and the give result by adding all 
# the element in result as by default
# if no function  passed it will add always
result = itertools.accumulate(GFG2.difference(GFG1))
  
# printing each item from list
for each in result:
    print(each)


Output:

0
4
11


Previous Article
Next Article

Similar Reads

Prefix sum array in Python using accumulate function
We are given an array, find prefix sums of given array. Examples: Input : arr = [1, 2, 3] Output : sum = [1, 3, 6] Input : arr = [4, 6, 12] Output : sum = [4, 10, 22] A prefix sum is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence {a, b, c, ...} are a, a+b, a+b+c and so on. We can solve this problem
1 min read
Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python - Itertools.tee()
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 tee() function This iterator splits the container into a number of it
2 min read
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 you want to multiply their elements. There can be sever
13 min read
itertools.groupby() in Python
Prerequisites: 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. Itertools.groupby() This method calculates the keys for each element prese
2 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 repeat() itertools.repeat()falls under the category
2 min read
Python - Itertools.takewhile()
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 easy. One such itertools function is takewhile(). Note: For more information, refer to Python Itertools takewhile() This allows considering an item from the iterable until t
3 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 Compress() The itertools.compress() falls under the
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 information, refer to Python Itertools Dropwhile() The d
1 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. This module provides various functions that work
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg