Python - Itertools.accumulate() - GeeksforGeeks

31 Mar, 2020
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 Listfor each in colors:    print(each)
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
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.
Syntaxitertools.accumulate(iterable[, func]) –> accumulate object
This function makes an iterator that returns the results of a function.
Parametersiterable & function
Now its enough of the theory portion lets play with the code
Code: 1
# import the itertool module # to work with itimport itertools  # import operator to work # with operatorimport operator  # creating a list GFGGFG = [1, 2, 3, 4, 5]  # using the itertools.accumulate() result = itertools.accumulate(GFG,                               operator.mul)  # printing each item from listfor each in result:    print(each)
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 itimport itertools  # import operator to work with# operatorimport operator  # creating a list GFGGFG = [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 resultresult = itertools.accumulate(GFG, max)  # printing each item from listfor each in result:    print(each)
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 itimport itertools    # creating a set GFG1 and GFG2GFG1 = { 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 alwaysresult = itertools.accumulate(GFG2.difference(GFG1))  # printing each item from listfor each in result:    print(each)
0
4
11

srisrinu
Python-itertools
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
Iterate over a list in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python