Dictionaries and Sets¶
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:
chapman = {"name": "Graham", "age": 48,
"Jobs": ["Comedian", "Writer"] }
chapman
print(chapman["Jobs"])
print(chapman["age"])
print(type(chapman))
Keys and Values¶
The things we can use to look up with are called keys:
chapman.keys()
The things we can look up are called values:
chapman.values()
When we test for containment on a dict
we test on the keys:
"Jobs" in chapman
"Graham" in chapman
"Graham" in chapman.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 = {
["Lamb", "Mint"]: True,
["Bacon", "Chocolate"]: False
}
Supplementary material: You can start to learn about the 'hash table'. Though this video is very advanced, 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())
Safe Lookup¶
Some times you want a program to keep working even when a key is looked up but it's not there.
Python dictionaries offers that through the get
method.
x = {"a": 1, "b": 2}
x["a"]
x["fish"]
x.get("a")
x.get("fish")
By default get
returns None
if the key searched is not in the dictionary.
However, you can change that default by adding what's the value you want it to return.
x.get("fish", "tuna") == "tuna"
Sets¶
A set is a list
which cannot contain the same element twice.
We make one by calling set()
on any sequence, e.g. a list or string.
name = "Graham Chapman"
unique_letters = set(name)
unique_letters
Or by defining a literal like a dictionary, but without the colons:
primes_below_ten = { 2, 3, 5, 7}
print(type(unique_letters))
print(type(primes_below_ten))
unique_letters
This will be easier to read if we turn the set of letters back into a string, with join
:
print("".join(unique_letters))
join
uses the character give to be what joins the sequence given:
"-".join(["a", "b", "c"])
Note that a set 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)
Set operations work as in mathematics:
x = set("Hello")
y = set("Goodbye")
x & y # Intersection
x | y # Union
y - x # y intersection with complement of x: letters in Goodbye but not in Hello
Your programs will be faster and more readable if you use the appropriate container type for your data's meaning. Always use a set for lists which can't in principle contain the same data twice, always use a dictionary for anything which feels like a mapping from keys to values.