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
# 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) # -------------------------------------------------------------------------
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!
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
2 жыл бұрын
Thanks, I will add that to my python quiver
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.
as ususal the best content delivered to us, thx !!!
Awesome! I didn’t know that this existed! I’m definitely going to use this more in my code!
Brief and to the point, as usual!
Thank you for the brief explanation.
Thanks for your contribution!
Wow, I became enlightened, thank you, great video.
Woow, that is the most comprehensive comprehension of dictionary comprehensions that I have comprehensively watched so far! Kudos! Thank you so much!
Thank you very much for this tutorial, it's very short and i learned a lot of things 😄
Great work, bro, thanks! 👌🏻
Bro code you are the best! Thanks for sharing all you're knowledge and making everything simple
Very nice tutorial. Thanks a lot. This is my first video in this channel. I liked this channel and will watch all Python videos.
very good lesson
Great video, thanks
Thank you👏👏
thx 4 vid bro !
You make it seem easy
Very nice tutorial!
Thank you!
Great tutorial!
This channel is gold 🥰
Thanks Bro !
thanks, bro!
Thanks!
You’re the man bro
nice vid
thx bro
Awesome bro 👍👍
thanks bro
a good video as always
superb
thanks
Superb explanation 👌 👏
Thanks Bro
nice :)
Wow!
TY bRO
Appreciated
Thank you
love u
Thanks
👍👍👍
W
Great explanation…. If I can understand, any Tom, Dick and Harry can. Thank you, Bro.
meow~!🐱
what plugin do you have to make lists green in your terminal?
@aletranqui9631
Жыл бұрын
you can change the color in the settings of pycharm
what's the difference between the if/else statement at the beginning and at the end
big preesh
BroooO!
Random Comment ❤
so this is like map fuction?
drop a comment
i love you
Can a key have multiple associated values / is there a way to do it? e.g. key1: 32, 36, 38
@Ulissescars
Жыл бұрын
Only if the value is a list/tuple
@kychemclass5850
Жыл бұрын
@@Ulissescars Tq.
support for the channel
.
Yo...bro how can i thank you.🥹
Thanks !!!
thanks bro
thanks