Skip to main content

Dictionaries

Dictionaries​

birth_years = {"kevin": 2008, "kate": 2007, "nvi": 2006}
# so the Thriller,... is the key
# the 1980, 1982,... is the value
print("kevin" in birth_years)
Output
True
tip

Do not name a variable dict: it hides Python's built-in dict() function.

.values()​

birth_years = {"kevin": 2008, "kate": 2007, "nvi": 2006}
print(birth_years.values())
Output
dict_values([2008, 2007, 2006])

.keys()​

birth_years = {"kevin": 2008, "kate": 2007, "nvi": 2006}
print(birth_years.keys())
Output
dict_keys(['kevin', 'kate', 'nvi'])
scores = {"D": (4, 4, 4)}
print(scores["D"])
Output
(4, 4, 4)
birth_years = {"kevin": 2008, "kate": 2007, "nvi": 2006}
print(list(birth_years.keys()))
Output
['kevin', 'kate', 'nvi']