How to Use the reduce() Function
Since Python 3, reduce()
has gone from a built-in function to a functools
module function. As you saw with map()
and filter()
, its first two arguments are respectively a function and an iterable.
It may also take an initializer as a third argument that is used as the initial value of the resulting accumulator. For each element of the iterable, reduce()
applies the function and accumulates the result that is returned when the iterable is exhausted.
00:00
Finally, in this section: reduce()
. As you can see, reduce()
also takes a function
and an iterable
, like map()
and filter()
do, but it works in a different way again.
00:12 It applies a function with two arguments cumulatively to the items of a sequence from left to right to reduce the sequence to a single value. It allows cumulative application of a function to every element of an iterable.
00:27
With the advent of Python 3, reduce()
was moved to the functools
module. You can see it being imported here. In the second line, you can see the numbers 1
, 2
, 3
, and 4
are being passed in in a list, and the reduce()
function’s lambda
takes x
and y
and returns the sum of x
and y
.
00:50
The end result is a single variable total
, which sums to 10
. As you’ve just seen, reduce()
applies a function cumulatively to the items of a sequence.
01:05
The first thing we need to do is to import it, as it’s now been moved into the functools
module. Secondly, generate a list of numbers. And in this case, we’re going to create a total
using a simple lambda expression that adds two values together.
01:32
It needs to take two values in, as this is the way reduce()
works, and we’re just going to add them together. Finally, the iterable that needs to be passed in—in this case, the list nums
—and then printing the total
out.
01:59
There you see the total of the numbers from 1
to 10
has been done. Now, the eagle-eyed amongst you will realize that this is merely a reconstruction of the sum()
function,
02:18 and you can see it confirmed there. The sums are the same.
02:27
So here, we’re going to change x + y
to x * y
and multiply that list of numbers together. Clearly, any other mathematical function could be applied cumulatively to a list by altering the lambda function.
02:49
Lambda functions and reduce()
don’t solely need to work on numbers. They can work on text as well, as any other kind of data. Here’s a quick example of creating a concatenation of the first two characters of a list of names. Once more, we’ll start by importing reduce()
from functools
, and then create the list of names that we want to work with—in this case, some of the people who contribute to realpython.com.
03:27
Next, we’ll use reduce()
to apply our lambda function across the entire list.
03:37
And here, the lambda function is going to take the entirety of one string and then add just the first two characters of the second string, and we’re going to pass it our list of names
. Finally, we’ll print out our result.
04:08
And there you can see it’s nearly worked, though something slightly unusual has happened. The first entry has been given in its entirety, and this is one of the quirks of reduce()
, but there is a way around this.
04:23
This can be changed by giving the optional parameter here, which is the first value. So this will be passed in first, and then Dan’s name will be treated normally as our y
with just the first two characters. Now we’re running the program again.
04:45
You can see we’ve got the interesting moniker dadajojach
, which is how we will now be referred to.
Become a Member to join the conversation.