Here are resources and additional documentation about the operator module and operators:
Combining map() With reduce()
Let’s now talk about combining the
map() function with the
reduce() function. The
reduce() function is another important function in functional programming, and in Python, it’s contained in the
functools module. Just like the
filter() function, it takes two positional arguments.
I’ll talk about this idea of recursively applying the function in the next slide. So, for example, let’s suppose we have this iterable of a list containing four elements
d, then a call to the
reduce() function with the function
f() is simply going to be obtained as follows.
We first apply the function
f() to the first two elements of the iterable,
b. Then we take that return value as the first positional argument to another call of
f() with the second positional argument being the third element of the iterable—in this case,
c. Then we take that return value as the first positional argument to another call with the function
f() and the second positional argument being the last element of the iterable—in this case,
d. So, for example, if the function is just simple addition of elements, which—depending on the context—may not just be addition of numbers, then a call to the
reduce() function will take
f() and simply return
a + b, and then that as the first positional argument to the sum with
c, and then that return value as the first positional argument, adding it up to
So, the way to think about the
reduce() function is that it applies the function from left to right, taking two arguments at a time. The first two arguments are going to be the first two elements of the iterable, and then take that return value and apply it with the third element of the iterable and so on.
reduce() also accepts a third parameter. It’s optional and it’s usually called the
initializer and it’s going to provide a seed value to the computation or it serves as a default value when the iterable is empty. And by seed value we mean that it’s going to serve as the first positional argument on the left of the first call to the
reduce() function and the second argument will be the first element of the iterable.
Maybe first you want to do some sort of mapping of a function on an iterable and then either apply the
reduce() method or the
filter() function, and so a lot of times you may want to combine these two functions. All right, let’s jump to the example.
04:05 The problem that you want to solve is you want to only extract from this list the unique elements or the unique numbers represented by these strings and you want to convert them into integer data types.
So at the end of the day, what you want is either maybe an iterator or maybe a list that would contain, for example, the number
12, the number
33, the number
33 is included already, so
12, and then
32 as well, right?
04:37 So, that’s the idea: extract all of the elements from the list that are unique, or just keep the only unique copies of them, and just make them into integers. Now, there’s a lot of ways for you to do this but let’s just do this using function calls so that we sort of remain in the spirit of functional programming.
The way I’m thinking of doing this is why don’t we first flatten out this list? And if you think about the operation of flattening out a list, you can think of it as a
reduce() call because whenever I add to a list, I’m concatenating them. And so if I add the first two, it basically creates a list containing the first two lists, and then I take that list and I add it to the third, and that would create a list containing all of these elements in the first three lists, and then I take that list and I add it to the last one.
That is essentially what we just sort of discussed: this operation of flattening out is equivalent to applying the add operator by reducing the list via the add operator. Now, of course this is going to be a problem if we just call
reduce() like this, because we need a function object, right?
We need a function that does addition. Now, we can write our own
lambda function to do this but maybe this is a good time—if you haven’t had a chance to explore the
operator module—to use the
add() function contained in the
operator module. And the
operator module, it contains a whole bunch of functions, sort of the basic intrinsic operators that are used all over in Python. So when you’re doing comparisons, you’re using the less than or equal operator (
<=), well, instead of using the symbol
<= you can call in the
lt() (less than) operator in the
operator module, right?
So, anyways, what we want to do is from the
operator module we want to import the
add() operator. And again, if you haven’t used the
operator module, there’ll be a link at the bottom here and you can check it out. So, let’s import
add(). All right.
Let’s only keep the unique elements in that list. And again, there’s lots of ways to do this. Why don’t we use the
set() method? Because the
set() method will take that iterable, that list, and will create a
set out of it.
Any repetitions won’t be repeated because a set, by definition, is a container where all the elements are unique. And so that will create an iterable. It’ll be a set. Let’s now map the
int() function on that so that we obtain only integer data types at the end. All right!
All right! So, if all you want now is to iterate over
b, then you can just keep it as a
map object, but for us to see what happened here, let’s just call the
list() method on that. And there we go!
So, this is sort of a good example of what functional programming is about. You’re doing everything using function calls. The first thing we did was we reduce the list to get a single list that’s not nested, and then we keep only the unique elements in that single list using the
set() function, and then we map the integer built-in function so that we get just integers. Now, in some cases instead of using the
reduce() function, you’re better off using some of the built-in Python functions because you’ll end up getting a lot more readable code.
So, if you did actually want to sum some numeric data, then of course instead of using
reduce() and the
add() operator with a list of numbers, you would just call and use the
sum() function on that.
Maybe they solve a problem that you want to essentially do by some sort of reduction process. So again, alternative tools out there that you may want to try that are already part of the Python library. Okay! Well, that’s it on the
reduce() method. In the next lesson, we’re going to take a look at the
starmap() function, which you’ll see is very similar to the
map() function but has some very nice use cases that you’ll want to know about.
Become a Member to join the conversation.