Dictionaries
intermediate25 minLearning objectives
- Explain key–value data storage
- Create and manipulate Python dictionaries
- Perform lookup, insertion and deletion operations
- Evaluate when dictionaries are more appropriate than arrays
Learn
AQA 4.2.10 — Dictionaries
Retrieval: the Fields, Records and Files lesson used a Python dictionary to model a single record's fields ({"id": "S001", "name": "Aisha", "score": 82}). This lesson looks properly at the dictionary ADT itself: a key-value store, where every value is looked up by a unique key rather than by position.
contacts = {"Aisha": "07700900001", "Tom": "07700900002"}
print(contacts["Aisha"]) # lookup by key - "07700900001"
contacts["Priya"] = "07700900003" # insertion - adds a new key-value pair
contacts["Tom"] = "07700900099" # also insertion syntax, but updates an existing key
del contacts["Aisha"] # deletion - removes a key-value pair entirely
Common mistake
Every key in a dictionary must be unique — assigning to an existing key doesn't create a duplicate, it silently overwrites the previous value (as contacts["Tom"] = ... did above). Also worth knowing: modern Python dictionaries happen to preserve the order items were inserted in, but the ADT itself is not meant to be accessed by position — always look items up by key, not by assuming a particular order, since relying on incidental ordering is fragile and isn't how AQA specifies the ADT.
Worked example — a contact management system
def add_or_update(contacts, name, number):
contacts[name] = number # works whether name is new or already exists
def remove_contact(contacts, name):
if name in contacts:
del contacts[name]
return True
return False
contacts = {}
add_or_update(contacts, "Aisha", "07700900001")
add_or_update(contacts, "Tom", "07700900002")
print(contacts)
remove_contact(contacts, "Tom")
print(contacts)
Trace it
Starting from an empty dictionary, trace: add_or_update("Aisha", "111"), add_or_update("Tom", "222"), add_or_update("Aisha", "999"), remove_contact("Tom"). State the dictionary's contents after each step.
(After step 1: {"Aisha": "111"}. After step 2: {"Aisha": "111", "Tom": "222"}. After step 3: {"Aisha": "999", "Tom": "222"} — Aisha's number was overwritten, not duplicated. After step 4: {"Aisha": "999"}.)
Choose and justify — dictionaries vs arrays
An array is the right choice when you need every item, in order, and don't need to find one specific item quickly by something other than its position. A dictionary is the right choice when you need to look something up directly by a meaningful identifier — a name, an ID, a word — without scanning through everything else first. Searching an array of 10,000 records for one match (linear search, Sequence 5) checks records one at a time; looking the same record up in a dictionary by key is effectively instant, regardless of how large the dictionary is.
Cumulative retrieval — from Sequence 1
remove_contact above only removes a contact if name in contacts — that condition is a Boolean expression, evaluating to True or False, exactly the relational/Boolean concept from Sequence 1's Relational and Boolean Operators lesson, now applied to a completely different kind of value (dictionary membership rather than comparing two numbers). Write a similar guarded function, update_if_exists(contacts, name, number), that updates a contact's number only if that name is already a key — leaving the dictionary completely unchanged if it isn't — and explain in one sentence why the Boolean check is doing exactly the same logical job here as it did back in Sequence 1, just on a different data type.
Challenge
Extend the contact management system with a search function that returns a contact's number given their name, returning "Not found" if the name isn't a key in the dictionary — then use add_or_update, remove_contact and your new search together to build, modify and query a small contacts dictionary.
Looking ahead: Sequence 5 (Fundamental Algorithms) revisits searching directly — you'll be able to compare linear search's step-by-step scanning against a dictionary's near-instant key lookup with real evidence, not just this lesson's claim.