Python dictionary comprehension 🕮

Ғылым және технология

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/stores/bro-code-5
===========================================================
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...
===========================================================

Пікірлер: 68

  • @BroCodez
    @BroCodez3 жыл бұрын

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

  • @razvanandreivalcu9649
    @razvanandreivalcu96492 жыл бұрын

    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!

  • @adamg2491
    @adamg24912 жыл бұрын

    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 жыл бұрын

    Thanks, I will add that to my python quiver

  • @TheZKes
    @TheZKes Жыл бұрын

    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.

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 Жыл бұрын

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

  • @fredericoamigo
    @fredericoamigo Жыл бұрын

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

  • @aristoncatipay1555
    @aristoncatipay1555Ай бұрын

    Brief and to the point, as usual!

  • @sayanbose1667
    @sayanbose16677 ай бұрын

    Thank you for the brief explanation.

  • @cristiucvladimir7909
    @cristiucvladimir79092 ай бұрын

    Thanks for your contribution!

  • @milo_andrs
    @milo_andrs3 ай бұрын

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

  • @InfameKato
    @InfameKato2 ай бұрын

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

  • @RobloxBedwarsPlayer01
    @RobloxBedwarsPlayer018 ай бұрын

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

  • @EmilioAt77
    @EmilioAt77Ай бұрын

    Great work, bro, thanks! 👌🏻

  • @bryce3851
    @bryce38512 ай бұрын

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

  • @dinceryildirim
    @dinceryildirim Жыл бұрын

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

  • @nyiaung4963
    @nyiaung49633 ай бұрын

    very good lesson

  • @shepherdmakanza5953
    @shepherdmakanza59532 жыл бұрын

    Great video, thanks

  • @mohammedchriatt4137
    @mohammedchriatt41373 жыл бұрын

    Thank you👏👏

  • @bekturasanbekov1979
    @bekturasanbekov1979 Жыл бұрын

    thx 4 vid bro !

  • @user-dj9gg1sq9q
    @user-dj9gg1sq9q5 ай бұрын

    You make it seem easy

  • @calsbergus
    @calsbergus Жыл бұрын

    Very nice tutorial!

  • @SuperStarEevee
    @SuperStarEevee2 жыл бұрын

    Thank you!

  • @mjprescotti
    @mjprescotti3 ай бұрын

    Great tutorial!

  • @kapibara2440
    @kapibara24408 ай бұрын

    This channel is gold 🥰

  • @loveSG999
    @loveSG9993 жыл бұрын

    Thanks Bro !

  • @sasasasasasasa5374
    @sasasasasasasa53743 жыл бұрын

    thanks, bro!

  • @soulharm5532
    @soulharm55322 жыл бұрын

    Thanks!

  • @koru615
    @koru6155 ай бұрын

    You’re the man bro

  • @t3hav8r
    @t3hav8r8 ай бұрын

    nice vid

  • @mslightsite
    @mslightsite Жыл бұрын

    thx bro

  • @achrajsaxena938
    @achrajsaxena9389 ай бұрын

    Awesome bro 👍👍

  • @volleynoob
    @volleynoob Жыл бұрын

    thanks bro

  • @riceball7238
    @riceball72386 ай бұрын

    a good video as always

  • @engr.malikiftikharahmad8751
    @engr.malikiftikharahmad87518 ай бұрын

    superb

  • @rubenc4696
    @rubenc46962 ай бұрын

    thanks

  • @sravankumar1767
    @sravankumar176711 ай бұрын

    Superb explanation 👌 👏

  • @frankgeter9392
    @frankgeter93922 жыл бұрын

    Thanks Bro

  • @carterjackson7460
    @carterjackson74603 жыл бұрын

    nice :)

  • @beingzero7541
    @beingzero75412 жыл бұрын

    Wow!

  • @lw9954
    @lw9954 Жыл бұрын

    TY bRO

  • @John-ex3xc
    @John-ex3xc Жыл бұрын

    Appreciated

  • @uuhju7004
    @uuhju70043 жыл бұрын

    Thank you

  • @HussainAli-sb1dv
    @HussainAli-sb1dv8 ай бұрын

    love u

  • @soulharm5532
    @soulharm55322 жыл бұрын

    Thanks

  • @farkhodmirzabekov9294
    @farkhodmirzabekov9294 Жыл бұрын

    👍👍👍

  • @dododifwasssup1784
    @dododifwasssup1784 Жыл бұрын

    W

  • @All-The-Best-To-You
    @All-The-Best-To-YouАй бұрын

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

  • @gustavoaponte1814
    @gustavoaponte1814 Жыл бұрын

    meow~!🐱

  • @ToastFreak777
    @ToastFreak7772 жыл бұрын

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

  • @aletranqui9631

    @aletranqui9631

    Жыл бұрын

    you can change the color in the settings of pycharm

  • @ahwe173
    @ahwe173Ай бұрын

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

  • @Craulback
    @Craulback2 жыл бұрын

    big preesh

  • @Ivobe2
    @Ivobe22 жыл бұрын

    BroooO!

  • @farhan8834
    @farhan8834 Жыл бұрын

    Random Comment ❤

  • @kubam1232
    @kubam12322 жыл бұрын

    so this is like map fuction?

  • @Adamelamine
    @Adamelamine2 жыл бұрын

    drop a comment

  • @mateozaccaria5312
    @mateozaccaria53122 жыл бұрын

    i love you

  • @kychemclass5850
    @kychemclass5850 Жыл бұрын

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

  • @Ulissescars

    @Ulissescars

    Жыл бұрын

    Only if the value is a list/tuple

  • @kychemclass5850

    @kychemclass5850

    Жыл бұрын

    @@Ulissescars Tq.

  • @jhassee
    @jhassee Жыл бұрын

    support for the channel

  • @ayyappadasmt3271
    @ayyappadasmt32717 ай бұрын

    .

  • @francisakash.x1494
    @francisakash.x14946 ай бұрын

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

  • @dzmitrymakarski613
    @dzmitrymakarski6132 жыл бұрын

    Thanks !!!

  • @ervinas4919
    @ervinas49192 жыл бұрын

    thanks bro

  • @mustaphakashit5811
    @mustaphakashit5811 Жыл бұрын

    thanks