Picture a real dictionary: you look up a word — the key — and immediately find its meaning — the value — without reading every entry from the start. Python dictionaries work exactly the same way. Once you understand that every item in a dictionary is a key-value pair, a whole world of efficient, flexible data storage opens up.
What is a dictionary in computing?
A dictionary (often abbreviated to dict in Python) is a data structure that stores items as key-value pairs. Rather than accessing items by a numbered position (index 0, 1, 2 …), you look them up by a meaningful key — usually a string or integer.
student = {
"name": "Priya",
"age": 14,
"school": "Westbrook Academy"
}
Here "name", "age", and "school" are keys; "Priya", 14, and "Westbrook Academy" are the corresponding values.
How do keys and values work?
Every key in a dictionary must be unique — you cannot have two entries with the same key (the second one overwrites the first). Keys must also be immutable, which is why strings, integers, and tuples are valid keys but lists are not.
Values, on the other hand, can be anything at all: numbers, strings, lists, even other dictionaries.
| Term | What it means | Example |
|---|---|---|
| Key | The label you look up | "name" |
| Value | The data attached to that label | "Priya" |
| Key-value pair | One entry in the dictionary | "name": "Priya" |
| Mutable? | Yes — add, change, or delete pairs | (unlike a tuple) |
How do you create a dictionary in Python?
Use curly braces {} with colons separating each key from its value, and commas between pairs:
capitals = {
"England": "London",
"Scotland": "Edinburgh",
"Wales": "Cardiff"
}
You can also build an empty dictionary and add items one at a time:
scores = {}
scores["Alice"] = 88
scores["Bob"] = 74
scores["Carol"] = 91
How do you access, update, and remove entries?
Accessing a value: use the key in square brackets.
print(capitals["Wales"]) # Cardiff
If the key might not exist, use .get() to avoid an error:
print(capitals.get("France", "Not found")) # Not found
Updating a value: simply assign a new value to an existing key.
scores["Alice"] = 95 # updates Alice's score
Deleting an entry: use del or .pop().
del scores["Bob"] # removes the "Bob" entry
removed = scores.pop("Carol") # removes and returns 91
What built-in methods do dictionaries have?
student = {"name": "Priya", "age": 14, "year": 9}
print(student.keys()) # dict_keys(['name', 'age', 'year'])
print(student.values()) # dict_values(['Priya', 14, 9])
print(student.items()) # dict_items([('name','Priya'),('age',14),('year',9)])
print("age" in student) # True — membership test on keys
print(len(student)) # 3
You can loop over a dictionary using for:
for key, value in student.items():
print(key, ":", value)
How do dictionaries compare with lists and tuples?
| Feature | List [ ] |
Tuple ( ) |
Dictionary { } |
|---|---|---|---|
| Access by | Index (0, 1, 2 …) | Index | Key (any immutable type) |
| Ordered? | Yes | Yes | Yes (Python 3.7+) |
| Mutable? | Yes | No | Yes |
| Allows duplicates? | Yes | Yes | Keys: No; Values: Yes |
| Best for | Ordered sequences | Fixed records | Labelled data / lookups |
Dictionaries shine when you need to look something up by name — such as finding a student's score, retrieving a country's capital, or counting how often each word appears in a text.
When would you choose a dictionary over a list?
The golden rule: if you find yourself writing comments like # index 0 is name, index 1 is age, switch to a dictionary. Named keys make your code self-documenting and far less error-prone.
# Poor approach — easy to mix up indices
student_list = ["Priya", 14, "Westbrook Academy"]
name = student_list[0]
# Better approach — self-documenting
student_dict = {"name": "Priya", "age": 14, "school": "Westbrook Academy"}
name = student_dict["name"]
Dictionaries are also the natural choice for frequency counting:
text = "the cat sat on the mat"
word_count = {}
for word in text.split():
word_count[word] = word_count.get(word, 0) + 1
print(word_count)
# {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}
Frequently asked questions
Are Python dictionaries ordered?
Since Python 3.7, dictionaries maintain the insertion order — items appear in the order you added them. In earlier versions of Python the order was not guaranteed, so older textbooks may describe them as unordered.
What happens if I access a key that does not exist?
Python raises a KeyError. To handle this safely, use .get(key, default_value) — if the key is not found it returns the default value instead of crashing.
Can a dictionary contain another dictionary?
Yes. This is called a nested dictionary and is very common for representing structured data. For example, a school register might store a dictionary per pupil, each containing name, year group, and a list of subjects.
Do I need to know dictionaries for the GCSE computer science exam?
Yes. AQA and OCR both include dictionaries in their specifications under data structures. You should be able to create a dictionary, access and update values by key, and explain the difference between a dictionary and a list in an exam scenario.
Struggling to remember when to use each data structure? Visit aitutors.me — Professor Turing will walk you through real exam questions step by step.