Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

00:00 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.

00:15 But basically, the reduce() function takes a function, a sequence, and then an initial value that I believe is optional, and it reduces the sequence

00:26 down to a single output value by applying this function repeatedly to the items

00:34 in this 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.

00:52 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.

01:06 We’re going to take this and we’re going to do some experimentation here with our reduce() function. The first thing I want to do here is I want to calculate the total age of this group.

01:18 If you remember what we did in the last installment of the series, we did something like this…

01:27 where we calculated this derived list that had the 'name' and 'age' of all of these

01:35 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.

02:24 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.

02:44 The return value of this function will be the new value of the accumulator. So, I know this sounds crazy, but

02:53 this allows us to do something like this, where we can say

02:58 “Okay, reduce this names_and_ages thing down to a single value using this rule here,

03:06 this lambda, and we’re going to start out with an initial value of 0 for the accumulator.” Let’s run this and then let’s take a look at the output.

03:18 Okay. 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.

03:38 So, initially the accumulator will be 0, and then it calls this lambda here with the first item.

03:47 And you can see here that we’re returning the current value of the accumulator, which is 0, plus the 'age' field of this item here that gets passed in.

03:58 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 202 and val is this item’s dictionary.

04:12 And now, we’re adding together the value of the accumulator and the value of the 'age' field in this dictionary, here, and we’re returning that. And so on.

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.

04:35 For example, I could have gone like this and said sum of x['age'] for x in names_and_ages and that would have done the same thing in an even more concise and nice representation.

Avatar image for breaves

breaves on March 29, 2020

So you’ve basically defined the function def f(acc,val): return acc+val[‘age’]

So each item of the sequence is always applied to the SECOND argument of the function, right? Could as well be def f(x,y): return x+y[‘age’] and it’s basically calling for s in sequence: x=f(x,s) return x

right?

Avatar image for Roy Telles

Roy Telles on July 24, 2020

If I’m not mistaken, reduce() can be viewed as doing the for loop iterations for us:

We can expand the reduce() function to be something like:

def total_age(val, acc=0):
    for item in val:
        acc += item['age']

    return acc
  • acc=0 is what reduce() does when we pass the optional initial value 0 to it
  • acc += item['age'] is what reduce() does for us, at its core
  • return acc is our ultimate value at the end of our iterations

Become a Member to join the conversation.