Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
How to Use the reduce() Function
Since Python 3,reduce() has gone from a built-in function to afunctools module function. As you saw withmap() andfilter(), 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:00Finally, in this section:reduce(). As you can see,reduce() also takes afunction and aniterable, likemap() andfilter() do,but it works in a different way again.
00:12It applies a function with two argumentscumulatively to the items of a sequence from left to rightto reduce the sequence to a single value.It allows cumulative application of a function to every element of an iterable.
00:27With the advent of Python 3,reduce() was moved to thefunctools module.You can see it being imported here. In the second line,you can see the numbers1,2,3, and4 are being passed in in a list,and thereduce() function’slambda takesx andy and returnsthe sum ofx andy.
00:50The end result is a single variabletotal, which sums to10.As you’ve just seen,reduce()applies a function cumulatively to the items of a sequence.
01:05The first thing we need to do is to import it,as it’s now been moved into thefunctools module.Secondly, generate a list of numbers.And in this case,we’re going to create atotal using a simple lambda expression that adds twovalues together.
01:32It needs to take two values in, as this is the wayreduce() works,and we’re just going to add them together.Finally, the iterable that needs to be passed in—in this case, the listnums—and then printing thetotal out.
01:59There you see the total of the numbers from1 to10 has been done. Now,the eagle-eyed amongst you will realize that this is merely a reconstruction ofthesum() function,
02:18and you can see it confirmed there. The sums are the same.
02:27So here, we’re going to changex + y tox * yand multiply that list of numbers together.Clearly, any other mathematical function could be applied cumulatively to a list byaltering the lambda function.
02:49Lambda functions andreduce() 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 charactersof a list of names. Once more, we’ll start by importingreduce() fromfunctools,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:27Next, we’ll usereduce() to apply our lambda function across the entire list.
03:37And here,the lambda function is going to take the entirety of one string and then addjust the first two characters of the second string,and we’re going to pass it our list ofnames. Finally,we’ll print out our result.
04:08And 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 ofreduce(), but there is a way around this.
04:23This 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 oury with just the first twocharacters. Now we’re running the program again.
04:45You can see we’ve got the interesting monikerdadajojach,which is how we will now be referred to.
Course Contents

