Python - List comprehension vs map function TUTORIAL (speed, lambda, history, examples)

Поділитися
Вставка
  • Опубліковано 23 гру 2024

КОМЕНТАРІ • 28

  • @opium9202
    @opium9202 Рік тому +2

    I thinks you should get more views. you are covering the most important topics and related questions. Thanks for the video

  • @ninesquared81
    @ninesquared81 4 роки тому +5

    I think one time to use map over a list comprehension is when you don't actually need a list.
    For example, I have a function that takes 24 hour time as a string and converts it to the number of minutes from 00:00 for that time.
    The way my function works is to unpack the 24 hour time into two variables hrs and mins which are integers.
    Using map (as I have done):
    def minutes(str_time):
    hrs, mins = map(int, str_time.split(':'))
    return hrs*60 + mins
    Using list comprehension:
    def minutes(str_time):
    hrs, mins = [int(x) for x in str_time.split(':')]
    return hrs*60 + mins
    To me, use of map here is more intuitive and probably faster, although speed isn't really my concern here.
    This is my first time using map since I started to learn Python in February 2020.
    If I want a list I'll of course use list comprehension because it is generally clearer and doesn't require calling the list function.

    • @mythuszeus
      @mythuszeus 2 місяці тому

      Year 2024. I wonder how much you've learned about python, most probably advanced to some higher level languages?

    • @ninesquared81
      @ninesquared81 2 місяці тому

      @mythuszeus I haven't really used anything higher level than Python. I have tried Haskell a couple of times but never really got anywhere with it.
      I have gone the other way, though, with C and a little assembly. My current project is a programming language compiler, written in C, and with x86 assembly as the output.

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

    THANK YOU FOR THIS BRENDAN!!!

  • @brianglass8310
    @brianglass8310 2 роки тому +1

    The history and best practices discussion was very helpful

  • @mohammadjunaidmansoori9879
    @mohammadjunaidmansoori9879 Рік тому +2

    May be this way
    num = [1, 2, 3, 4]
    lst = list(map(str, filter(lambda x: x > 1, num)))
    print(lst)

  • @gabrielamos9772
    @gabrielamos9772 2 роки тому +1

    What editor are you using?

  • @Morexod999
    @Morexod999 2 роки тому +1

    Hello! What is this editor\high-lighting software? It looks cool

  • @akira_asahi
    @akira_asahi 2 роки тому +1

    Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.

  • @Fine_Mouche
    @Fine_Mouche 2 роки тому +2

    Ok, but how use the map without convert it ? which objects accept a map object ?

  • @rb9888
    @rb9888 11 місяців тому +1

    7:37 - it would be better to use filter first and then map ;-)

  • @jell._.y
    @jell._.y 8 місяців тому +1

    Lolol the creator not liking map, goated

  • @Rocklee46v
    @Rocklee46v 4 роки тому

    Great, you have explained everything.

  • @aviralsrivastava7197
    @aviralsrivastava7197 4 роки тому

    great video!!!!

  • @yekhtiari
    @yekhtiari Рік тому +1

    I didn't get why you used map(int,[1,2,3,4]). The list was already int type.

  • @sehishok6664
    @sehishok6664 3 роки тому

    Look man ..my English is weak but you have very nice way inserting information in brain... thank you

  • @OldOneX
    @OldOneX 3 роки тому

    تستاهل الف لايك ...شكرا من 💔

  • @kingmoon989
    @kingmoon989 3 роки тому

    استمر ياوحش

  • @scott9503
    @scott9503 3 роки тому

    you sound like casually explained