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