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

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 พ.ย. 2024

ความคิดเห็น • 30

  • @LucasRibeiro
    @LucasRibeiro 7 ปีที่แล้ว +15

    The defaultdict tip was awesome!

    • @realpython
      @realpython  7 ปีที่แล้ว +1

      Thanks for the kind words!

    • @derallli1
      @derallli1 6 ปีที่แล้ว

      Defaultdict is what i would expect from a language that strives to be elegant. Groupby is horrible unless i use multiple comprehensions to get what i expect for a grouping: a Dictionary/Map. How did they come up with returning lists of tuples ???

  • @stansmoltis5881
    @stansmoltis5881 6 ปีที่แล้ว +11

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

  • @kevklash
    @kevklash 2 ปีที่แล้ว

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

  • @felipealvarez1982
    @felipealvarez1982 2 ปีที่แล้ว

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

  • @user-wc1sm8cj8s
    @user-wc1sm8cj8s 3 ปีที่แล้ว +3

    how did you add autocomplete to your python interpreter??

    • @0_-
      @0_- 3 ปีที่แล้ว

      yes i was wondering too

  • @joshuagardner2030
    @joshuagardner2030 2 ปีที่แล้ว

    Thank you for making this!

  • @joels2923
    @joels2923 4 ปีที่แล้ว

    tonnes of great stuff in this playlist. Thank You!

  • @Abhi-ms8pk
    @Abhi-ms8pk 7 ปีที่แล้ว +2

    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.

    • @realpython
      @realpython  7 ปีที่แล้ว +1

      Glad you liked it. Thanks for the suggestion! :-)

  • @canht95
    @canht95 5 ปีที่แล้ว +1

    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?

  • @abhishekgaikwad7318
    @abhishekgaikwad7318 4 ปีที่แล้ว +1

    Could you please explain final reduce() version.

  • @1s9b8b7
    @1s9b8b7 7 ปีที่แล้ว +3

    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.

    • @realpython
      @realpython  7 ปีที่แล้ว

      Nice one! Thanks for sharing your solution with us :)

  • @marcelmarceli8246
    @marcelmarceli8246 3 ปีที่แล้ว

    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.

  • @srisuman3996
    @srisuman3996 4 ปีที่แล้ว

    This guy is awesome

  • @hugodsa89
    @hugodsa89 6 ปีที่แล้ว

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

    • @realpython
      @realpython  6 ปีที่แล้ว

      Here is a list of resources I use dbader.org/resources/

  • @eashwarinfosys9856
    @eashwarinfosys9856 4 หลายเดือนก่อน

    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

  • @kejor8617
    @kejor8617 4 ปีที่แล้ว

    Can you add this list here?

  • @jameslovering9158
    @jameslovering9158 5 ปีที่แล้ว +1

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

    • @realpython
      @realpython  5 ปีที่แล้ว

      I hope the video helped clear it up for you!

  • @igormorgado
    @igormorgado 3 ปีที่แล้ว

    What is this Python REPL?

  • @MarkusBurrer
    @MarkusBurrer 3 ปีที่แล้ว

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

  • @Fahrooooo
    @Fahrooooo 6 ปีที่แล้ว

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

  • @TadasTalaikis
    @TadasTalaikis 7 ปีที่แล้ว

    Excellent.

    • @realpython
      @realpython  7 ปีที่แล้ว

      Glad you liked it!

  • @seapearl3175
    @seapearl3175 7 หลายเดือนก่อน

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