Python dictionary comprehension 🕮

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • Python dictionary comprehension tutorial example explained
    #python #dictionary #comprehension
    -------------------------------------------------------------------------
    dictionary comprehension = create dictionaries using an expression
    can replace for loops and certain lambda functions
    # dictionary = {key: expression for (key,value) in iterable}
    dictionary = {key: expression for (key,value) in iterable if conditional}
    dictionary = {key: (if/else) for (key,value) in iterable}
    dictionary = {key: function(value) for (key,value) in iterable}
    -------------------------------------------------------------------------
    cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
    cities_in_C = {key: round((value-32)*(5/9)) for (key,value) in cities_in_F.items()}
    print(cities_in_C)
    -------------------------------------------------------------------------
    weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': "cloudy"}
    sunny_weather = {key: value for (key,value) in weather.items() if value == "sunny"}
    print(sunny_weather)
    -------------------------------------------------------------------------
    Bro Code merch store 👟 :
    ===========================================================
    teespring.com/...
    ===========================================================
    music credits 🎼 :
    ===========================================================
    Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
    Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
    Free Download / Stream: bit.ly/2JnDfCE
    Music promoted by Audio Library • Up In My Jam (All Of A...
    ===========================================================

Комментарии • 71

  • @BroCodez
    @BroCodez  3 года назад +27

    # dictionary comprehension = create dictionaries using an expression
    # can replace for loops and certain lambda functions
    #
    # dictionary = {key: expression for (key,value) in iterable}
    # dictionary = {key: expression for (key,value) in iterable if conditional}
    # dictionary = {key: (if/else) for (key,value) in iterable}
    # dictionary = {key: function(value) for (key,value) in iterable}
    # -------------------------------------------------------------------------
    cities_in_F = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
    cities_in_C = {key: round((value-32)*(5/9)) for (key,value) in cities_in_F.items()}
    print(cities_in_C)
    # -------------------------------------------------------------------------
    # weather = {'New York': "snowing", 'Boston': "sunny", 'Los Angeles': "sunny", 'Chicago': "cloudy"}
    # sunny_weather = {key: value for (key,value) in weather.items() if value == "sunny"}
    # print(sunny_weather)
    # -------------------------------------------------------------------------
    # cities = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
    # desc_cities = {key: ("WARM" if value >= 40 else "COLD") for (key,value) in cities.items()}
    # print(desc_cities)
    # -------------------------------------------------------------------------
    # def check_temp(value):
    # if value >= 70:
    # return "HOT"
    # elif 69 >= value >= 40:
    # return "WARM"
    # else:
    # return "COLD"
    # cities = {'New York': 32, 'Boston': 75, 'Los Angeles': 100, 'Chicago': 50}
    # desc_cities = {key: check_temp(value) for (key,value) in cities.items()}
    # print(desc_cities)
    # -------------------------------------------------------------------------

  • @TheZKes
    @TheZKes Год назад +4

    Welp. Your explaination for an comprehensions is more clear for me (the guy who knows english on intermediate level) than explanations in my university in my native language.
    Thank you Bro dude.

  • @RobloxBedwarsPlayer01
    @RobloxBedwarsPlayer01 11 месяцев назад +1

    Thank you very much for this tutorial, it's very short and i learned a lot of things 😄

  • @EmilioAt77
    @EmilioAt77 3 месяца назад

    Great work, bro, thanks! 👌🏻

  • @milo_andrs
    @milo_andrs 5 месяцев назад

    Wow, I became enlightened, thank you, great video.

  • @adamg2491
    @adamg2491 3 года назад +17

    This formula might be useful if you want to create a new dictionary with different both keys and values:
    dictionary = {expression1(key) (if/else) : expression2(value) (if/else) for (key,value) in iterable}
    Example:
    A = {'a':1,'b':2,'c':3}
    B = {key.upper() : (value**2) for (key,value) in A.items()}
    print(A)
    output: {'a': 1, 'b': 2, 'c': 3}
    print(B)
    output: {'A': 1, 'B': 4, 'C': 9}

    • @frankgeter9392
      @frankgeter9392 2 года назад +1

      Thanks, I will add that to my python quiver

  • @razvanandreivalcu9649
    @razvanandreivalcu9649 2 года назад +10

    Nice job. I'm learning python and this vid was just what I needed for an assignment. I understand list/dictionary comprehensions now, awesome. Thank you for your work!

  • @kapibara2440
    @kapibara2440 10 месяцев назад

    This channel is gold 🥰

  • @volleynoob
    @volleynoob Год назад

    thanks bro

  • @fredericoamigo
    @fredericoamigo Год назад +2

    Awesome! I didn’t know that this existed! I’m definitely going to use this more in my code!

  • @mslightsite
    @mslightsite Год назад +2

    thx bro

  • @t3hav8r
    @t3hav8r 10 месяцев назад

    nice vid

  • @SuperStarEevee
    @SuperStarEevee 2 года назад

    Thank you!

  • @ahwe173
    @ahwe173 3 месяца назад

    what's the difference between the if/else statement at the beginning and at the end

  • @mjprescotti
    @mjprescotti 5 месяцев назад +1

    Great tutorial!

  • @dinceryildirim
    @dinceryildirim Год назад +1

    Very nice tutorial. Thanks a lot. This is my first video in this channel. I liked this channel and will watch all Python videos.

  • @francisakash.x1494
    @francisakash.x1494 8 месяцев назад +2

    Yo...bro how can i thank you.🥹

  • @All-The-Best-To-You
    @All-The-Best-To-You 4 месяца назад

    Great explanation…. If I can understand, any Tom, Dick and Harry can. Thank you, Bro.

  • @mohammedchriatt4137
    @mohammedchriatt4137 3 года назад

    Thank you👏👏

  • @InfameKato
    @InfameKato 5 месяцев назад

    Woow, that is the most comprehensive comprehension of dictionary comprehensions that I have comprehensively watched so far!
    Kudos! Thank you so much!

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 Год назад +1

    as ususal the best content delivered to us, thx !!!

  • @uuhju7004
    @uuhju7004 3 года назад

    Thank you

  • @calsbergus
    @calsbergus Год назад +1

    Very nice tutorial!

  • @bryce3851
    @bryce3851 5 месяцев назад

    Bro code you are the best! Thanks for sharing all you're knowledge and making everything simple

  • @ToastFreak777
    @ToastFreak777 2 года назад +1

    what plugin do you have to make lists green in your terminal?

    • @aletranqui9631
      @aletranqui9631 Год назад

      you can change the color in the settings of pycharm

  • @shepherdmakanza5953
    @shepherdmakanza5953 2 года назад +1

    Great video, thanks

  • @sasasasasasasa5374
    @sasasasasasasa5374 3 года назад +1

    thanks, bro!

  • @HanySalem
    @HanySalem 15 дней назад

    Thank you

  • @rishidixit7939
    @rishidixit7939 Месяц назад

    Why .items method used ?

  • @beingzero7541
    @beingzero7541 2 года назад +1

    Wow!

  • @aristoncatipay1555
    @aristoncatipay1555 3 месяца назад

    Brief and to the point, as usual!

  • @sayanbose1667
    @sayanbose1667 9 месяцев назад

    Thank you for the brief explanation.

  • @ahmedabdulgawad4758
    @ahmedabdulgawad4758 9 дней назад

    great content

  • @farhan8834
    @farhan8834 2 года назад

    Random Comment ❤

  • @cristiucvladimir7909
    @cristiucvladimir7909 5 месяцев назад

    Thanks for your contribution!

  • @dododifwasssup1784
    @dododifwasssup1784 Год назад

    W

  • @gustavoaponte1814
    @gustavoaponte1814 Год назад

    meow~!🐱

  • @rubenc4696
    @rubenc4696 5 месяцев назад

    thanks

  • @HussainAli-sb1dv
    @HussainAli-sb1dv 10 месяцев назад

    love u

  • @engr.malikiftikharahmad8751
    @engr.malikiftikharahmad8751 10 месяцев назад

    superb

  • @lw9954
    @lw9954 2 года назад

    TY bRO

  • @mustaphakashit5811
    @mustaphakashit5811 Год назад

    thanks

  • @kubam1232
    @kubam1232 3 года назад

    so this is like map fuction?

  • @Adamelamine
    @Adamelamine 2 года назад

    drop a comment

  • @sravankumar1767
    @sravankumar1767 Год назад

    Superb explanation 👌 👏

  • @ayyappadasmt3271
    @ayyappadasmt3271 9 месяцев назад

    .

  • @kychemclass5850
    @kychemclass5850 Год назад

    Can a key have multiple associated values / is there a way to do it? e.g. key1: 32, 36, 38

  • @jhassee
    @jhassee Год назад

    support for the channel

  • @ervinas4919
    @ervinas4919 3 года назад

    thanks bro

  • @riceball7238
    @riceball7238 8 месяцев назад

    a good video as always

  • @koru615
    @koru615 8 месяцев назад

    You’re the man bro

  • @user-dj9gg1sq9q
    @user-dj9gg1sq9q 7 месяцев назад

    You make it seem easy

  • @nyiaung4963
    @nyiaung4963 5 месяцев назад

    very good lesson

  • @achrajsaxena938
    @achrajsaxena938 11 месяцев назад

    Awesome bro 👍👍

  • @Ivobe2
    @Ivobe2 3 года назад

    BroooO!

  • @frankgeter9392
    @frankgeter9392 2 года назад

    Thanks Bro

  • @Craulback
    @Craulback 2 года назад

    big preesh

  • @mateozaccaria5312
    @mateozaccaria5312 3 года назад

    i love you

  • @John-ex3xc
    @John-ex3xc Год назад

    Appreciated

  • @soulharm5532
    @soulharm5532 3 года назад

    Thanks

  • @dzmitrymakarski613
    @dzmitrymakarski613 2 года назад

    Thanks !!!

  • @soulharm5532
    @soulharm5532 3 года назад

    Thanks!

  • @bekturasanbekov1979
    @bekturasanbekov1979 Год назад

    thx 4 vid bro !

  • @loveSG999
    @loveSG999 3 года назад

    Thanks Bro !

  • @farkhodmirzabekov9294
    @farkhodmirzabekov9294 2 года назад

    👍👍👍

  • @carterjackson7460
    @carterjackson7460 3 года назад

    nice :)