In this blog, I will explain how to work with the python reduce function. We will understand the working of reduce function with multiple examples. so let’s get started.
Python reduce function
In python, a reduce function is used to perform functional programming. Reduce function takes a pre-defined function and applies the logic to all the elements.
The reduce function is very useful in processing iterables without the need to define loops. The reduce function is similar to for loop in python. Reduce function can be applied to the list, dictionary, etc.
reduce function implements a mathematical technique commonly known as folding or reduction. Reduce function fold or reduced the list of items to a single value.
Syntax of Python reduce function
The reduce function takes three parameters. Below is the syntax of the python reduce function.
functools.reduce(function, iterable[, initializer]) where: function: function which will be applied to all the elements in an iterable iterable: python objects that can be iterated/looped over(lists, tuples, sets,etc) initializer: If the optional initializer is present, it is placed before the items of the iterable in the calculation.
Function and iterable are mandatory parameters whereas the initializer is the optional parameter.
Python reduce examples
In this session, I will explain the working of the python reduce function with various examples.
Python reduce list
Let’s understand how we can use the reduce function in a list of items. We will use the reduce function to calculate the sum of items present in the list.
Let’s first define a function that will add the elements together.
def add_fun(a,b): return a+b
Now we will be using this add_fun() to find the sum of elements in the list l.
l = [1,2,3,4,5,6,7,8] from functools import reduce reduce(add_fun,l)
the above code returns 36 as the output.
Python reduce lambda function
In this session, we will understand how to use the lambda function within the reduce function to calculate the sum of items present in the list.
from functools import reduce l = [1,2,3,4,5,6,7,8] reduce(lambda a,b: a+b ,l)
We have passed the lambda function inside the reduce function. The lambda function can take two inputs and give the sum as the output.
python reduce with an initializer
Let’s understand how we can pass the optional parameter in the reduce function to calculate the sum of elements present in list l.
from functools import reduce import operator l = [1,2,3,4,5,6,7,8] reduce(operator.add,l,100)
Since we have passed 100 as the initializer, so the reduce function will add the elements in the list starting from 100.
Python reduce to find the max
The Max function can be used inside the reduce function to calculate the maximum elements present in the list. We will be using the lambda function to calculate the max. Below is the code for the same.
from functools import reduce l = [1,2,3,4,5,6,7,8] reduce(lambda a,b:a if a > b else b,l)
The max element present in list l is 8. Hence the above code returns 8 as an output.
Python reduce accumulator
In the python, itertools module there is one more function called accumulator() is present. Often people are confused about the reduce accumulator function.
The accumulator() function takes an iterable as an input. If func is supplied, it should be a function of two arguments. Below is the syntax of the accumulator function.
functools.reduce(function, iterable[, initializer])
Let’s understand the working of an accumulator with the below examples.
from itertools import accumulate l = [1,2,3,4,5,6,7,8] list(accumulate(l))
The above code gives the below output
[1, 3, 6, 10, 15, 21, 18, 36]
I hope you have liked this tutorial on the python reduce function. Please do let me know if you need additional input.