Why Use the reduce() Function?
Now that you know the basics of how the
reduce() function works, you’re going to look into some of the interesting ways in which you can use this building block of functional programming. You’ll see how the
reduce() function allows you to group your data set into arbitrary categories.
You’ll also learn about Python’s
defaultdict class, which is defined in the
collections module. Next, you’ll familiarize yourself with some useful helpers in the
itertools module, such as
At this point, you might be wondering, “Okay, why do I even need to use the
reduce() function in this case at all?” And the reason is that the
reduce() function—it can actually go far beyond what you’ve seen here.
We can talk about some pretty crazy examples here. Why don’t we look at some more interesting uses of the
reduce() function? All right, so one interesting thing that I thought we could do with the
reduce() function is grouping scientists by field.
We can do that by creatively using the accumulator field. All right, so what we’re going to do here is we’re going to define a function that we can pass to the
reduce() function that will take our existing list of scientists and
assign it to these fields and group people that way. The way we’re going to do it—there is actually a way you can do this in a single
lambda expression, but I just started recording that example and I was like, “Man, this actually really detracts from what I want to show you here.” So here, I’m just going to define my
reducer() function and it’s going to take the
accumulator and a value. I’m just going to say, okay, in the accumulator, well, you want to look at the
.field that the scientist belongs to and then we’re just going to append the
.name of that particular scientist.
And then, of course, we need to return the accumulator. You might be wondering, “What is that? What does that do?” You’re going to see that in a minute. So here, I’m now typing out the
reduce() function call, so I need to pass my
reducer() function, and I need to pass my
scientists, and then I need to initialize the accumulator. And so here, this is where this thing comes in because we need to make sure that this dictionary here actually has slots, or has these keys in here for all the different fields, so that the
reducer() can go over it and it can update the accumulator according to the individual field that each of these scientists belongs to. All right. So now, once I’ve run this…
let’s pretty-print it out. And yeah, you can see here it worked! So, for
'astronomy', you’ve got
'physics'—they’re all kind of sorted that way and assigned to the right categories.
And I hope you can see how this worked here with this
reducer() function, and the main thing you need to figure out is how—what I was struggling with the most was how these names like accumulator and value, how they correspond to actual items and what the accumulator looks like as it gets updated. Right?
One thing that I really don’t like about this is that I have to give it a list of categories upfront that then gets populated. That’s kind of stupid. I mean, if I make a mistake, you know, I have a typo here, then it’s just all going to blow up. But there is a better way to do it, and that is the
defaultdict class in the
Then, what that will do—I’ll show you that in a minute. What that will do is it will lead to the same result because this
.defaultdict() thing is pretty magical.
call on, it knows that it has this field in there, in the
defaultdict, and it doesn’t need to recreate it. So, this is a little trick you can use to get around having to manually define the accumulator here. But of course, it also adds more complication to this function call and I’ll bet you this would actually take many people a while to figure out and to read that, and so that’s actually a pretty good reason to not use that in production code.
05:41 However, I think it makes for a really interesting thought exercise in Python and I think it’s pretty cool to work with these different kinds of programming paradigms and programming styles, like functional programming, object-oriented programming, and more, like procedural programming.
05:59 And it makes sense to be comfortable with these different styles, because even if you don’t always stick to any one style, you know, 100% of the time, you’re going to learn a lot about when these things have their strength and when you should apply them. I think that is really valuable and that’s going to put you above and beyond what most people do when they learn programming, right? So, I think there’s a lot of value in exploring these things.
Become a Member to join the conversation.