What Is the reduce() Function?
In this lesson, you’ll learn how to use the
reduce() function (or
functools.reduce() in Python 3), which is one of the functional programming primitives (or building blocks) available in Python. It takes a function, a sequence (or iterable), and an optional initial value.
You’ll walk through how you can use the
reduce() function with the data set that you’ve been using in this course to reduce a sequence to a sinlge output value by applying a function repeatedly in order to output a single value.
All right! So, let’s go over this. The
reduce() function—it takes a function and then it takes a sequence of stuff. I believe for the
map() function and for the
filter() function, those were called iterables, so the naming is a little bit inconsistent here, I guess.
sequence. So, the way that’s expressed in the docstring is a little bit more complicated, but we’re going to take a look at some examples here that actually go back to our data set. All right, so just to make sure you see what I’m talking about here, this is the original data set that we were working with.
It is an immutable data structure. This is a tuple of other
namedtuples, so each item here is a
namedtuple. That way, we can make sure I can’t reach in here and accidentally modify any of these items or the whole structure. All right.
scientists in 2017. I’m actually going to use this derived list for the first example here for using the
reduce() function. What we want to do here is we want to calculate the
total_age and for that, we’re going to use the
reduce() function and we’re going to define a lambda function.
01:57 I could also do this outside with a regular function, but here with the lambda I think it’s appropriate, because then, you know, it could be really declarative and it could all just fit into one short expression. So here, what I’m going to do is I’m going to define a lambda that takes two arguments.
02:12 The first one is the accumulator, The second one is the value. Let me type this out and then I’m going to talk about what these individual parts mean and how they work. Maybe you can figure it out as I’m typing out this function.
So, this lambda function is going to be applied repeatedly to all the items in this list.
val will always be the latest item that we’re looking at, and the accumulator will be, sort of, a running variable that gets updated repeatedly.
807. So, this should be the sum of all of these ages here. Let’s talk about how this worked. What you can see here is we have this lambda function that is the function that the
reduce() call applies to all of these items. And the way it does it, it picks an initial value for the accumulator that I set here.
And then in the next round, well, the value of the accumulator was updated—so at this point it would be
202. And then in the next round—we’re taking this item, here—the value of the accumulator is
val is this item’s dictionary.
04:21 So, this is how we boil down this whole list into a single integer that represents the sum of all of these names. Now, in Python, again, there are other ways you would represent this particular calculation.
Become a Member to join the conversation.