= {}
my_empty_dict print(type(my_empty_dict))
<class 'dict'>
A dictionary datatype is a container for many values, in which each value is described by a named attribute known as a "key". In this way, a dictionary is said to have "key value pairs".
A dictionary has squiggly braces ({}
) on the extremities, and comma-separated key-value pairs inside, where each key is separated from it’s corresponding value by a colon (:
).
A dictionary may have zero or more key-value pairs.
= {}
my_empty_dict print(type(my_empty_dict))
<class 'dict'>
= {
dog "breed": "Lab",
"name": "Murphy",
"weight_lbs": 45,
"age_yrs": 6,
"well_behaved": True
}print(type(dog))
print(dog)
<class 'dict'>
{'breed': 'Lab', 'name': 'Murphy', 'weight_lbs': 45, 'age_yrs': 6, 'well_behaved': True}
In order to access the values inside, we’ll need to reference the corresponding "key". So whenever we are working with a dictionary, it will be helpful to first ask, "what are its keys?". Sometimes we can do this visually. However especially in the case of nested structures, it may be helpful to explicitly ask for the key names using the dictionary’s keys
method (with trailing parentheses because it is a method):
dog.keys()
dict_keys(['breed', 'name', 'weight_lbs', 'age_yrs', 'well_behaved'])
In the same way, we can access just the values:
dog.values()
dict_values(['Lab', 'Murphy', 45, 6, True])
Or finally, we can access the key-value pairs as associated tuples:
dog.items()
dict_items([('breed', 'Lab'), ('name', 'Murphy'), ('weight_lbs', 45), ('age_yrs', 6), ('well_behaved', True)])
We use square brackets to access a specific value, by designating the name of that value’s key:
print(dog["breed"])
print(dog["name"])
#print(dog["fav_food"]) # INVALID (KeyError)
Lab
Murphy
Adding items to a dictionary:
"best_friend"] = "Bella"
dog[
print(dog["best_friend"])
Bella
Updating items in a dictionary:
print(dog["age_yrs"])
"age_yrs"] = 7
dog[
print(dog["age_yrs"])
6
7
Removing items from a dictionary:
print(dog.keys())
del dog["best_friend"]
print(dog.keys())
dict_keys(['breed', 'name', 'weight_lbs', 'age_yrs', 'well_behaved', 'best_friend'])
dict_keys(['breed', 'name', 'weight_lbs', 'age_yrs', 'well_behaved'])
It is common to encounter nested structures, where one object is nested inside another. Let’s consider the following example (dictionary of dictionaries):
= {
summary_profile 'AAPL': {
'address1': 'One Apple Park Way',
'city': 'Cupertino',
'state': 'CA',
'zip': '95014',
'country': 'United States',
},'GOOGL': {
'address1': '1600 Amphitheatre Parkway',
'city': 'Mountain View',
'state': 'CA',
'zip': '94043',
'country': 'United States',
},'MSFT': {
'address1': 'One Microsoft Way',
'city': 'Redmond',
'state': 'WA',
'zip': '98052-6399',
'country': 'United States',
} }
If we have a nested structure, we start on the extremities and go down one level at a time, to reach the value we are interested in:
# chained accessors:
print(summary_profile["AAPL"]["address1"])
One Apple Park Way
# one step at a time (equivalent):
= summary_profile["AAPL"]
apple = apple["address1"]
address print(address)
One Apple Park Way