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

Поділитися
Вставка
  • Опубліковано 11 лис 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.

  • @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].

  • @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 Рік тому

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

    • @ThatSayanGuy
      @ThatSayanGuy 11 місяців тому +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 11 місяців тому

      @@ThatSayanGuy Generators is best idea.

  • @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

  • @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 7 місяців тому

      Just make your function a generator ​@@DrDeuteron

  • @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?

  • @mahdirasouli6005
    @mahdirasouli6005 9 місяців тому

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

  • @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

  • @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.

  • @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!

  • @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 ?

  • @viniciusromano1298
    @viniciusromano1298 Рік тому

    "why_tho" read my mind🤣

  • @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

  • @redestroyer7994
    @redestroyer7994 Рік тому

    Basically std::accumulate

    • @RedHair651
      @RedHair651 7 місяців тому

      Accumulate also exists in python and it's different

  • @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.

  • @chriskeo392
    @chriskeo392 Рік тому

    Guido wants to get rid of map reduce

    • @Indently
      @Indently  Рік тому

      Guido wants to do many things ;)

  • @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.

  • @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 UA-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, UA-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 7 місяців тому

      That's easily something you can look up