Python Reduce Function

Explore the Python reduce() function visually. See the efficiency of cumulative computations on sequences, streamlining your code with this powerful tool.

What is the reduce function in Python?

  • The reduce function is used for cumulative computation on a sequence of values.
  • You  have to import functools Module before using reduce function
  • The reduce function takes two required and an optional parameter as follows:
functools.reduce(function, iterable[, initializer])

Working of reduce Function

This is how the reduce function works:

  • It will apply the given function of two arguments to the items of iterable e.g. a list. It applies function cumulatively.
  • The items of the sequence are entertained from left to right and the function will “reduce” the sequence to a single value.

I will explain how initialize argument works, first consider the following piece with two arguments:

lst = [5,10,20,40]

product = reduce(lambda x, y: x*y, lst)

The reduce will calculate as follows:

(((5*10)*20)*40)

If no initialize argument is given then the first item from the list is used as x and multiplied by y. This is followed by multiplication by next item with the accumulated result and so on. So, the above code should result:

40000

That is:

5 * 10 = 50

50 * 20 = 1000

1000 * 40 = 40000

So, the end result is reduced to a single value from the list i.e. 40000.

If the third argument (initializer) is given then it is placed before the items of sequence i.e. acts as x. For example:

lst = [5,10,20,40]

product = reduce(lambda x, y: x*y, lst, 5)

In that case, this is how the calculation is done:

5 * 5 = 25

25 * 10 = 250

250 * 20 = 5000

5000 * 40 = 200000

If the given sequence (in the above case, a list) was empty then this initializer would act as the default value.

Still now clear? See the next section with the code and output to make things clearer.

A simple example of reduce function

In this example, the reduce function is used with the lambda function.

In the reduce function, lambda acts as the first argument while a list of int items acts as the second. No third item is given, so the first item of the list acts as x.

See how we get the sum of that list of items:

#Getting sum of a list by reduce

from functools import reduce

int_lst = [10,20,50,100,1000]

lst_sum = reduce(lambda x, y: x+y, int_lst)

print ("The sum of list items = ", lst_sum)

The result:

The sum of list items =  1180

The example of using initializer argument

In this example, I am using a tuple rather than a list. Besides, the third argument (initializer) is also given to act as the first value (x) in the reduce function.

Have a look:

#Getting sum of a tuple by reduce


from functools import reduce

int_tupl = (10.5,20.4,50.5,100.4,75.6)

tup_sum = reduce(lambda x, y: x+y, int_tupl, 100)

print ("The sum of tuple items = ", tup_sum)

The result:

The sum of tuple items =  357.4

Using operator with reduce function example

In this example, a list of string items is concatenated by using the reduce function with the operator.

In the reduce function, the operator.add is passed as the function while the second argument is a list that contains string items.

See the code and output below:

#Reduce with operator.add
import functools, operator

str_list = ("This ", "is ", "string ", "concat ", "by ", "reduce")

concat_list = functools.reduce(operator.add, str_list)

print(concat_list)

The output:

This is string concat by reduce
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!