AQA 7517, section 4.2.7.1
A dictionary is a collection of key-value pairs, where each value is accessed using its associated key. Just as a real dictionary lets you look up a word (the key) to find its meaning (the value), a program dictionary lets you look up a value without knowing where it is stored.
- Each key is unique. You can't have the same key twice.
- The value can be any type, and can be repeated.
- You access a value by its key, not by a position number.
Using a dictionary in Python
ages = {"Asha": 17, "Ben": 18, "Chloe": 17}
print(ages["Ben"]) # 18 look up by key
ages["Dev"] = 16 # add a new pair
ages["Asha"] = 18 # change an existing value
del ages["Chloe"] # remove a pair
print("Ben" in ages) # True is this key present?
print(len(ages)) # 3
for name in ages:
print(name, ages[name])
Looking up a key that doesn't exist raises a KeyError, so check first with in, or use ages.get("Zed", "unknown"), which gives a default instead.
Dictionaries are often implemented using a hash table (4.2.6.1), which is why looking up a key is so fast.
Dictionary compared with a list
| List/array | Dictionary | |
|---|---|---|
| Accessed by | Position number (index) | Key |
| Order matters | Yes | No, you use keys, not positions |
| Typical question | "What is the 3rd item?" | "What goes with this key?" |
| Lookup speed | Searching for a value is slow | Finding the value for a key is fast |
Simple applications
Information retrieval
A common use is to count how often each word appears in a document. This is the basis of how search tools find relevant pages. The document "The green, green grass grows", ignoring letter case, is represented as:
{"grass": 1, "green": 2, "grows": 1, "the": 1}
Each word is a key and the number of times it appears is the value.
Other uses
- A phone book: name to number.
- A translation table: English word to French word.
- Settings: option name to chosen value.
- Storing a record's fields:
{"name": "Asha", "age": 17}.
Worked example
Task: count how many times each word appears in a sentence.
sentence = "the green green grass grows"
counts = {}
for word in sentence.split():
if word in counts:
counts[word] = counts[word] + 1
else:
counts[word] = 1
print(counts)
# {'the': 1, 'green': 2, 'grass': 1, 'grows': 1}
For each word: if it's already a key, add 1 to its count, and otherwise create the key with a count of 1. After "green" appears twice, its value is 2.
Exam tip: describe a dictionary as a collection of key-value pairs where the value is accessed via the key. If asked for an application, give a specific one, such as "counting word frequencies for information retrieval".
Check yourself
What is a dictionary?
A collection of key-value pairs in which each value is accessed using its associated key.
Can two entries in a dictionary have the same key? Can they have the same value?
No for the key, because keys are unique. Yes for the value, because different keys can map to the same value.
What would {"a": 1, "b": 2}["b"] give?
2.
Why is a dictionary better than a list for looking up a phone number from a name?
A dictionary goes straight to the value using the key (name), while a list would have to be searched item by item until the name was found.