► Write better & cleaner code using Python’s advanced features

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.

“reduce()” (or “functools.reduce()” on Python 3) is one of the functional programming primitives or building blocks available in Python and it’s useful in a number of contexts.

Later in the video you’ll also see how the reduce function lets you group your data set into arbitrary categories. You’ll also learn about Python’s “defaultdict” class defined in the collections module, as well as some useful helpers in the “itertools” module, like “itertools.groupby”.

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:

* Immutable Data Structures in Python:
*
* List comprehensions:

FREE COURSE – “5 Thoughts on Mastering Python”

SUBSCRIBE TO THIS CHANNEL:

* * *

► Python Developer MUGS, T-SHIRTS & MORE:

► PythonistaCafe – A peer-to-peer learning community for Python developers:

FREE Python Coding Tutorials & News:
» Python Tutorials:
» Python News on Twitter:
» Weekly Tips for Pythonistas:
» Subscribe to this channel:

Nguồn: https://shaarique.com/

Xem thêm bài viết khác: https://shaarique.com/cong-nghe/

10 Comments

Joel S

July 3, 2020

tonnes of great stuff in this playlist. Thank You!

Reply

Can

July 3, 2020

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?

Reply

James Lovering

July 3, 2020

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

Reply

Hugo Santos

July 3, 2020

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

Reply

Stanislav Smoltis

July 3, 2020

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) }

Reply

Fahrudin Halilović

July 3, 2020

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

Reply

Lucas Ribeiro

July 3, 2020

The defaultdict tip was awesome!

Reply

1s9b8b7

July 3, 2020

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.

Reply

Night Crawler

July 3, 2020

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.

Reply

Tadas Talaikis

July 3, 2020

Excellent.

Reply

Leave a Reply