Functional Programming in Python: The 'reduce()' Function

preview_player
Показать описание

In this Python tutorial you'll discover another basic of functional programming, namely how to use the "reduce()" function to transform data structures.

We'll take an example data set represented using an immutable data structure from the previous videos in this series, and then we'll create a "reduced" or "derived" output from that data using Python's built-in reduce function.

This video is part of a series of Python functional programming tutorials that I'm recording so stay tuned for the next instalment.

Be sure to check out these additional articles and Python tutorials if you want to dive deeper into functional programming:

* * *

FREE Python Coding Tutorials & News:
Рекомендации по теме
Комментарии
Автор

did someone notice that there is no Maria under physics field in scientists_by_field5 snippet? This is because the iterable for itertools.groupby() must be sorted to produce correct result. You can also make it more readable by unpacking groupby() result inside of the dictionary comprehension
scientists_by_field5 = { k : list(map(lambda x: x.name, g)) for k, g in itertools.groupby(sorted(scientists, key=lambda x: x.field), lambda x: x.field) }

stansmoltis
Автор

I absolutely loved this! The examples and the explanation were on point. You got a new subs, thanks for sharing!

kevklash
Автор

how did you add autocomplete to your python interpreter??

wcsmcjs
Автор

Thank you for putting **readability** above shorter lines (code-fu)

felipealvarez
Автор

Could you please explain final reduce() version.

abhishekgaikwad
Автор

My attempt to get scientists_by_field using lambda:
reduce(lambda acc, val: (acc[val.field].append(val), acc)[1], scientists, defaultdict(list))
I think it's more readable and it should be faster than Dan's solution, since every iteration creates 1 tuple and updates existing dictionary, while he creates 2 new dictionaries. I wish I came up with upacking solution tho.

sbb
Автор

In your final version of the reduce function implementation you wrote this line:
lambda acc, val: {**acc, **{val.field: acc[val.field] + [val.name]}}, ...
When you could do this:
lambda acc, val: {**acc, val.field: acc[val.field] + [val.name]}, ...


Am I missing something?

canht
Автор

tonnes of great stuff in this playlist. Thank You!

joels
Автор

Hey Dan,
As always, great video. Would you please do consider video on search, trees and linked list in future please. I found lot of sources but they are very hard to understand in programming way. Thank you.

Abhi-mspk
Автор

Iterable doesn't have to be a sequence, and reduce docs are explicit that you need a sequence not any iterable. This is not the inconsistency.

marcelmarceli
Автор

I made these changes to the code in my end.

from functools import reduce
instead of val['age'], I had to use val.age. Only then it worked

eashwarinfosys
Автор

what tools are you using to develop? the intellisense you get is so clean.

hugodsa
Автор

Look at this solution with empty dict and lambda function :
scientist_by_field = reduce(
lambda acc, val: if val.field in acc else acc.update({val.field:[val.name, ]}) or acc,
scientists,
{}
)

Fahrooooo
Автор

To be honest I prefer the reduce() version over the "pythonic" version.

MarkusBurrer
Автор

Oh that took me a few minutes to sink in how that works.

jameslovering
Автор

The defaultdict is basically the opposite of functional programming paradigm 😅 Even more dynamic than usual 🤣 But good to know

seapearl