Python dictionary comprehension 🕮

preview_player
Показать описание
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"}
# print(sunny_weather)

# -------------------------------------------------------------------------

Bro Code merch store 👟 :
===========================================================
===========================================================

music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
Рекомендации по теме
Комментарии
Автор

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

#

BroCodez
Автор

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!

razvanandreivalcu
Автор

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

piotrkopcewicz
Автор

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

InfameKato
Автор

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

milo_andrs
Автор

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}

adamg
Автор

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

fredericoamigo
Автор

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

RobloxBedwarsPlayer
Автор

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

bryce
Автор

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.

TheZKes
Автор

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

dinceryildirim