Dictionaries are data structures containing key-value pairs.
Dictionaries have a set of unique keys and are used to retrieve the value information associated with these keys.
For instance, a dictionary might be used to store:
Dictionaries are very common and are frequently used and encountered in practice.
Dictionaries are specified by curly braces, { }
, containing zero or more comma-separated key-value pairs. In each key-value pair the keys and values are separated by a colon, :
.
# Dictionary with four key value pairs
a_dict = {"a": 1, "b": 2, "c": 3, "d": 4}
# The a, b, c, d are keys
# The 1, 2, 3, 4 are values
print(a_dict)
# A key cannot be repeated
# See what happens when we repeat the key "c"
a_dict = {"a": 1, "b": 2, "c": 3, "d": 4, "c": 4}
print(a_dict)
Here is a more realistic dictionary. It contains three keys, "Panos", "Maria", and "John". Each of these keys has a value associated with it, which in the case below corresponds to a phone number.
phones = {
"Panos": "212-998-0803",
"Maria": "656-233-5555",
"John": "693-232-5776",
}
print(phones)
And here is another dictionary, with three keys, "ip", "logitude", and "latitude", which capture an IP address and its geolocation.
geoip = {"longitude": -73.9885, "latitude": 40.7317, "ip": "216.165.95.68"}
print(geoip)
To access elements in the dictionary we use the key in brackets, or the get()
command, as follows:
print(geoip["ip"])
# or, alternatively
print(geoip.get("ip"))
print(phones["Panos"])
# or, alternatively
print(phones.get("Panos"))
We can add an entry in the dictionary by assigning a value to a particular key. If the key already exists, the value assigned to that key gets updatd.
# Add a new key, "isp", with value "New York University"
geoip["isp"] = "New York University"
print(geoip)
# Update the valye for "John"
phones["John"] = "415-794-3423"
# Add a new key, "Elena", and the corresponding value
phones["Elena"] = "212-998-0803"
print(phones)
If we want to remove a key x
from the dictionary, the command dict.pop(x)
removes the key x
and its associated value from the dictionary
# Remove John from the phones dictionary
phones.pop("John")
print(phones)
Like the set, the easiest way to check if a particular key is in a dictionary is through the in
keyword:
"Panos" in phones
"Jose" in phones
Notice that the in
will not work if we try to find a value in the dictionary.
# The in does *not* work for values
"212-998-0803" in phones
Some common operations on dictionaries:
dict.keys()
: returns a list containing the keys of a dictionarydict.values()
: returns a list containing the values in a dictionaryphones = {
"Panos": "212-998-0803",
"Maria": "656-233-5555",
"John": "693-232-5776",
"Jake": "415-794-3423",
}
phones.keys()
sorted(phones.keys())
phones.values()
a_dict
and b_dict
a_dict
and b_dict
a_dict = {"a": 5, "b": 5, "c": 3, "c": 4}
b_dict = {"c": 5, "d": 6}
# your code here
# Lets find the common keys first
# Extract the keys from each dictionary
keys_a = a_dict.keys()
keys_b = b_dict.keys()
# Then compute the intersection
# Keys are guaranteed to be unique, so the dict_keys
# behaves like a set, and supports set operations
common_keys = keys_a & keys_b
print("Common keys", common_keys)
# Now let's repeat the process for values
values_a = a_dict.values()
values_b = b_dict.values()
# However, trying to compute the intersection of values
# will not work if we try to apply it naively.
# The values_a and values_b are not like sets, as
# they can contain duplicate values
# Uncomment the code below to try. You will get
# "TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'"
#
# values_a & values_b
# Instead, we have to convert the values_a and values_b
# variables into sets first, and then compute the intersection
common_values = set(values_a) & set(values_b)
print("Common values", common_values)