Dictionaries¶
The Python Dictionary¶
Python supports a container type called a dictionary.
This is also known as an "associative array", "map" or "hash" in other languages.
In a list, we use a number to look up an element:
names = "Martin Luther King".split(" ")
names[1]
In a dictionary, we look up an element using another object of our choice:
me = {"name": "James", "age": 39, "Jobs": ["Programmer", "Teacher"]}
print(me)
print(me["Jobs"])
print(type(me))
Keys and Values¶
The things we can use to look up with are called keys:
me.keys()
The things we can look up are called values:
me.values()
When we test for containment on a dict
we test on the keys:
"Jobs" in me
"James" in me
"James" in me.values()
Immutable Keys Only¶
The way in which dictionaries work is one of the coolest things in computer science: the "hash table". This is way beyond the scope of this course, but it has a consequence:
You can only use immutable things as keys.
good_match = {("Lamb", "Mint"): True, ("Bacon", "Chocolate"): False}
but:
illegal = {[1, 2]: 3}
Supplementary material: You can start to learn about the 'hash table'. Though this video is very advanced I think it's really interesting!
No guarantee of order¶
Another consequence of the way dictionaries work is that there's no guaranteed order among the elements:
my_dict = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
print(my_dict)
print(my_dict.values())
Sets¶
A set is a list
which cannot contain the same element twice.
university = "University College London"
unique_letters = set(university)
unique_letters
print("".join(unique_letters))
"".join(["a", "b", "c"])
It has no particular order, but is really useful for checking or storing unique values.
alist = [1, 2, 3]
is_unique = len(set(alist)) == len(alist)
print(is_unique)
Safe Lookup¶
x = {"a": 1, "b": 2}
x["a"]
x["fish"]
x.get("a")
x.get("fish")
x.get("fish", "tuna") == "tuna"