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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Why Use the reduce() Function?

Give Feedback

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 itertools.groupby.

Comments & Discussion

senatoduro8 on July 24, 2019

Dan, What is the difference between functional and procedural programming?

angelojwillems on March 28, 2020

Waw, I use the Counter function from collections as a means to the same end. Didn’t realize there was a defaultdict. From now on I’ll just use that instead.

darth88vader88 on April 1, 2020

I get this error with bpython console:

total_age = reduce(lambda acc,val: acc + val[‘age’],names_and_ages,0) Traceback (most recent call last): File “<input>”, line 1, in <module> total_age = reduce(lambda acc,val: acc + val[‘age’],names_and_ages,0) NameError: name ‘reduce’ is not defined

cellist on April 1, 2020

If you are on python3 you have to do a

from functools import reduce # only in Python 3

first

darth88vader88 on April 2, 2020

Thanks!

darth88vader88 on April 2, 2020

Hahaha! I missed that in the first video on reduce(). Dumb!

jignashreddy on April 3, 2020

there was an error when i used the same in lambda function. i’m not sure why ? lambda x,y:x[y.field].append(y.name) the error is TypeError: ‘NoneType’ object is not subscriptable

Zarata on May 7, 2020

In production, how do people remember that the argument to a function such as “reducer” takes an argument “map” and that the map values are list? Since Python isn’t strongly typed (is that a correct statement?) such functions will break if they aren’t called with arguments of correct type. Just something I realized / wondered as I thought about what’s going on in “reducer”, trying to wrap my head around it (your words, video midpoint).

Varun Vaddiparty on May 10, 2020

@jignash you get that error when x is None. Most likely you are not passing in the optional argument of reduce function.

Ivan Smalzer on June 8, 2020

defaultdict is one of the things that makes Python cool.

Another way to handle expanding accumulator would be explicitly check val.fields in reducer for each object and create new key if it doesn’t exist.

Peter on July 3, 2020

Couldn’t the same thing be achieved with just specifying an empty dictionary as the starting value.

Something like:

def group_scientists(acc, val): if val.field not in acc: acc[val.field] = {} acc[val.field].append(val.name) return acc

Granted default dict is somewhat simpler.

Peter on July 3, 2020

and I forgot to put that in code block

def group_scientists(acc, val):
    if val.field not in acc:
        acc[val.field] = {}
    acc[val.field].append(val.name)
    return acc

Become a Member to join the conversation.