How To Use "reduce()" In Python Tutorial (Functools)

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

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

  • @dk-ww3kp
    @dk-ww3kp ปีที่แล้ว +17

    Amazing video👏 I love map() filter() and reduce(), I use them every time I see an opportunity.

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

      I was asked about them in my interview.

    • @dk-ww3kp
      @dk-ww3kp ปีที่แล้ว

      @@sabrysm how did it go?

    • @sabrysm
      @sabrysm ปีที่แล้ว

      @@dk-ww3kp It went well (although I was not familiar with them) but I got the offer!

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

      Please use comprehensions instead, they're way more readable. map and filter give me flashbacks to my dark days of writing haskell.

    • @NoProblem76
      @NoProblem76 ปีที่แล้ว

      most of the time, list/generator/dict/set comprehensions are better choice than map/filter.

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

    Now with about a few years of experience doing Python professionally, I’d confidently say I would never use these kind of functions over list comprehension, for a few reasons.
    1, they’re difficult to read and maintain.
    2, they’re an extra functionality you’d need to import in.
    And 3, most importantly, rather than remembering these myriad of Python functions, it’s better to learn the core and use Google for the rest when and if you need.

    • @spaghettiking653
      @spaghettiking653 ปีที่แล้ว

      How is this the same as list comprehensions? List comprehensions are surely for performing a function in a loop and generating a list, whereas this is generating a single value from an existing list.

    • @DhavalAhir10
      @DhavalAhir10 11 หลายเดือนก่อน

      Don't use list comprehension, it will blast RAM of our computer. Takes so many memory.

    • @ThatSayanGuy
      @ThatSayanGuy 10 หลายเดือนก่อน +1

      @@DhavalAhir10 You should look up generators when used in list comprehension. And comprehension by itself doesn't eat up RAM, unless you have written a very poorly optimised loop.

    • @DhavalAhir10
      @DhavalAhir10 10 หลายเดือนก่อน

      @@ThatSayanGuy Generators is best idea.

  • @oida10000
    @oida10000 ปีที่แล้ว +2

    I like to add that itertools has an accumulate function, that does the same but gives you an iterable for the inbetween values. It has minor differences: the place of the iterable and the function are switched and the inital value is a pure keyword argument, still nice to have it, so:
    print(list(accumulate(range(1, 6), lambda a, b:a+b, initial=20)))
    will print [20, 21, 23, 26, 30, 35].

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

    please make a video about class methods and object methods(all kind of it ) and the differences between them just for clarification (and also usage of them in real situations)
    because the way you teach is amazing and thank you very much for your consideration

    • @Indently
      @Indently  ปีที่แล้ว

      I already did

  • @alexanderoverchenko5770
    @alexanderoverchenko5770 ปีที่แล้ว +12

    I use join to make a string out of a list of strings. Also I add format to do the same result as you got with "INIT". You have not persuaded me to use "reduce". :)

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

      If you can use inbuilt functions that are simple (such as sum()), prioritise that, once you start programming something more complex than hello world, you might experience a use for reduce here and there :)

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

      @@Indently No, I don't

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

      Interesthing to know it exists, there is always alternative

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

      so I get massive data files from each orbit of a satellite. Since it orbits 9 times per day, I want to create a "daily output". This could be a real pain. I've seen the dog crap Matlab code ppl have tried: it's unmaintable. So I overload "

    • @sirgalahamtroskipero4872
      @sirgalahamtroskipero4872 ปีที่แล้ว

      @@DrDeuteron is FORTRAN any good?

  • @Pawlo370
    @Pawlo370 ปีที่แล้ว +3

    So you can very easy count the ! I don't know how to say in english but when you understand 2! = 2, 3! = 6 etc. In polish it's silnia, 2 silnia, 3 silnia itd.

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

      ! = Factorial.

    • @elatedbento
      @elatedbento ปีที่แล้ว +2

      Yes, you can easily calculate a factorial, although I think there are faster versions to do so. The important part is to understand the concept since it has many more applications other than factorial 😊

    • @DrDeuteron
      @DrDeuteron ปีที่แล้ว

      @@elatedbento any factorial that is cpu limited is not going to fit into memory.

    • @RedHair651
      @RedHair651 6 หลายเดือนก่อน

      Just make your function a generator ​@@DrDeuteron

  • @viniciusromano1298
    @viniciusromano1298 ปีที่แล้ว

    "why_tho" read my mind🤣

  • @localhost69
    @localhost69 ปีที่แล้ว

    if you skipped all illegal moves in that chess game, the only legal move would be c3 (white's move). this is assuming that you still alternate when a move is skipped.

  • @robertobokarev439
    @robertobokarev439 ปีที่แล้ว

    As a C programmer, this has broken my mind for a second. Then I realized there're no macroses and inline functions in Python

  • @mahdirasouli6005
    @mahdirasouli6005 8 หลายเดือนก่อน

    Do you have an overall overview video on functools like itertools?

  • @HungLe-pk3oz
    @HungLe-pk3oz ปีที่แล้ว

    Thank you for your video. I just wonder why you don’t have these on Facebook.

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

      because Facebook is unpythonic in the extreme. I still can't believe Guido worked for The Borg.

  • @davidconiglio7577
    @davidconiglio7577 ปีที่แล้ว

    I am starting using Python, and the difficulty that I have is to see the list command or function to make a study of each .Were to look for ?

    • @DrDeuteron
      @DrDeuteron ปีที่แล้ว

      RTFM.
      read the manual.

  • @Oler-yx7xj
    @Oler-yx7xj ปีที่แล้ว

    Functools does seem to be interesting, waiting for more videos on them

    • @DrDeuteron
      @DrDeuteron ปีที่แล้ว

      wraps, partial, total_ordering, lru_cache, cached_property...
      YES!

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

    these type hints just clutter the code.
    No seriously. What if I want to reduce a list of np.poly1d instances, because I actually need hard math at work. I don't need a product function saying "I take floats", and I sure don't need floats if I am computing factorials. It's totally unpythonic. I mean what's next "fprintf"???? We need to keep this in check.

  • @chriskeo392
    @chriskeo392 ปีที่แล้ว

    Guido wants to get rid of map reduce

    • @Indently
      @Indently  ปีที่แล้ว

      Guido wants to do many things ;)

  • @redestroyer7994
    @redestroyer7994 ปีที่แล้ว

    Basically std::accumulate

    • @RedHair651
      @RedHair651 6 หลายเดือนก่อน

      Accumulate also exists in python and it's different

  • @sangchoo1201
    @sangchoo1201 ปีที่แล้ว

    I wish I could write these like
    [1, 2, 3].map(lambda x: x*2).reduce(lambda a, b: a * b)

    • @voxelfusion9894
      @voxelfusion9894 ปีที่แล้ว

      You can do nested list comprehensions, but I wouldn't recommend it lol.

    • @DrDeuteron
      @DrDeuteron ปีที่แล้ว

      do you mean:
      >>>(2).__rpow__(reduce(operator.mul, [1, 2, 3]))
      36
      ???

    • @sangchoo1201
      @sangchoo1201 ปีที่แล้ว

      @DrDeuteron the result is the same, but the expression is not the same one.
      if I change a * b to a + b, the result won't be the same.

    • @sangchoo1201
      @sangchoo1201 ปีที่แล้ว

      @@voxelfusion9894 how do I change reduce to list comprehension?

    • @DrDeuteron
      @DrDeuteron ปีที่แล้ว

      @@sangchoo1201 operator.add

  • @denischirtulov957
    @denischirtulov957 ปีที่แล้ว

    not clear why it is better then "map" . what is difference ?

    • @KanYatKin
      @KanYatKin ปีที่แล้ว

      Map returns list

    • @dk-ww3kp
      @dk-ww3kp ปีที่แล้ว +2

      Iterable*

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

      It actually returns a map object that works as an iterator, which is much more memory efficient.

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

    For a guy 3 years into python, you should consider showing us real projects on yt instead of just simple 3 line snippets

    • @Indently
      @Indently  ปีที่แล้ว

      I recommend a channel called neuralnine for projects

    • @Sinke_100
      @Sinke_100 ปีที่แล้ว

      @@Indently it's a decent channel, of all I follow I like best Mariya Sha on Python Simplified

    • @Indently
      @Indently  ปีที่แล้ว

      Comment: My favourite channel is another channel
      Indently: 🥲

    • @Sinke_100
      @Sinke_100 ปีที่แล้ว

      @@Indently Dude, I wouldn't stay subscribed if I generaly don't like the content, but I don't just troll around, you need consructive criticism sometimes, we all do, if people just praising you even is something is average, there is no room for grow. I generaly think you should put on few projects, why not, video quality is good and you know how to program, acually I like the way you write code more than neural nine (aside from type hinting 🙂)

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

      Yeah I'm just kidding, I appreciate constructive criticism. I'm actually taking a break from posting projects to TH-cam because I've gotten a few copyright claims from AI that found "logos" in my projects. The official WhatsApp team filed a copyright claim since I had their logo in one of my videos (but it was filed by an AI, that scans videos). And there's absolutely no way to get around it, or even appeal to that.
      And the other time I made a video regarding some cyber security practises, TH-cam gave me a strike creating a video that showed "potentially harmful" material. I tried to appeal to that, but they refused that one as well. So I kind of got bummed out, which is why I respect NeuralNine, he got a few strikes and is still going strong!
      Maybe in the near future I will create more projects on this channel, but recently I've just been posting projects on my Udemy course.

  • @yasinsk5924
    @yasinsk5924 ปีที่แล้ว

    Sir please explain class and objects with full details not in shorts please
    I am having exams to join a company which is having a big important question with class topic
    Please sir 🙏🙏🙏 it will help my career

    • @Indently
      @Indently  ปีที่แล้ว

      I have a full course at: indently.io

    • @RedHair651
      @RedHair651 6 หลายเดือนก่อน

      That's easily something you can look up