What is a dictionary and how does it relate to a hash table?
Understand a dictionary as an abstract data type of key-value pairs, its operations, how it is typically implemented using a hash table, and when a dictionary is appropriate.
A focused answer to AQA A-Level Computer Science 4.2.7, covering the dictionary abstract data type of key-value pairs, its operations, its typical implementation using a hash table, and when to use a dictionary.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
AQA wants you to describe a dictionary as an abstract data type that stores key-value pairs, list its operations, explain that it is usually implemented with a hash table, and say when a dictionary is the right choice.
The dictionary abstract data type
Typical operations are: add or update a (key, value) pair, look up the value for a key, delete a pair, and test whether a key is present. Because a dictionary is an abstract data type, the specification describes only this behaviour, not the internal storage; the same operations could in principle be backed by different structures, though a hash table is by far the most common.
phonebook = {"Alice": "0123", "Bob": "0456"}
phonebook["Carol"] = "0789" # add
number = phonebook["Alice"] # look up by key
The keys give the data meaning that a numeric index cannot. In a phonebook the key "Alice" tells you what the value represents, whereas an array index such as 7 is arbitrary. This is the conceptual heart of why dictionaries exist: they let you store and retrieve data by something humans care about.
Implementation with a hash table
It is worth keeping the two ideas separate in your mind: the dictionary is the interface (what you can do, expressed as operations on keys and values), while the hash table is one mechanism that delivers that interface efficiently. Exam questions often probe exactly this distinction.
When to use a dictionary
A dictionary is ideal when data is naturally accessed by a label rather than a position: looking up a customer record by account number, counting word frequencies, caching computed results so they are not recalculated, or representing a sparse mapping where most possible keys are absent. If you only need ordered data accessed by position, an array or list is simpler; if you need the data kept in sorted order, a balanced search tree may be more suitable than a hash-backed dictionary.
The sparse-mapping use is worth dwelling on because it shows the dictionary's memory advantage. Suppose you needed to record which of a billion possible product codes are in stock. An array indexed by product code would need a billion slots, almost all empty. A dictionary stores only the codes that are actually present, so its size grows with the data, not with the range of possible keys. This is why dictionaries are the natural representation for sparse data, configuration settings keyed by name, and any mapping where the set of keys is unpredictable or far smaller than the set of all possible keys.
Most modern programming languages provide a dictionary as a built-in type (Python dict, the maps and hash maps of other languages), and being a built-in does not change its status as an abstract data type: the language documents the operations and guarantees, and the runtime supplies an efficient hash-table implementation behind the scenes. When you answer an exam question you should describe the dictionary in terms of its operations on keys and values, and only mention the hash table when asked how it is implemented or why it is fast.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20193 marksA program needs to count how many times each word appears in a document. Explain why a dictionary is a suitable data structure for this task and describe how it would be used.Show worked answer →
A dictionary stores key-value pairs with unique keys, so each distinct word can be a key and its running count the value. Because look-up is by key, the program can find and update the count for any word directly rather than searching a list.
For each word read, the program checks whether the word is already a key: if it is, the value is incremented; if not, the word is added with a value of 1. At the end, each key holds the frequency of that word.
Markers reward identifying word as key and count as value, the unique-key property, and the read-check-update process that builds the counts.
AQA 20223 marksExplain the relationship between a dictionary and a hash table, and state why this implementation makes look-up by key efficient.Show worked answer →
A dictionary is an abstract data type: it describes the behaviour of storing key-value pairs and accessing values by key, without specifying how it is built. A hash table is a concrete data structure commonly used to implement a dictionary.
Look-up is efficient because the key is passed through a hashing algorithm that computes the array index where the value is stored, so the program goes straight to that location instead of searching. This gives average constant time, , access regardless of how many entries the dictionary holds, provided collisions are kept low.
Markers reward the abstract-versus-implementation distinction and the explanation that hashing the key gives direct, near constant-time access.
Related dot points
- Understand a hash table, the role of a hashing algorithm, how a key maps to an index, the meaning of a collision, and collision-resolution methods such as rehashing and chaining.
A focused answer to AQA A-Level Computer Science 4.2.6, covering hash tables, hashing algorithms, mapping a key to an index, collisions, load factor, and collision-resolution methods including rehashing and chaining.
- Understand arrays (one, two and three dimensional), records and fields, and the difference between static and dynamic data structures.
A focused answer to AQA A-Level Computer Science 4.2.1, covering one, two and three dimensional arrays, records and fields, indexing, and the difference between static and dynamic data structures.
- Understand trees as a connected, undirected graph with no cycles, the terms root, child, parent, leaf and subtree, and the structure and use of a binary search tree.
A focused answer to AQA A-Level Computer Science 4.2.5, covering trees as connected acyclic graphs, the terminology root, child, parent and leaf, binary trees, and the structure and use of a binary search tree.
- Understand the queue abstract data type, FIFO behaviour, linear, circular and priority queues, and the enqueue and dequeue operations using front and rear pointers.
A focused answer to AQA A-Level Computer Science 4.2.2, covering the queue abstract data type, FIFO behaviour, linear, circular and priority queues, and the enqueue and dequeue operations with front and rear pointers.
- Understand the stack abstract data type, LIFO behaviour, the push, pop and peek operations using a stack pointer, and the use of stacks for subroutine calls and recursion.
A focused answer to AQA A-Level Computer Science 4.2.3, covering the stack abstract data type, LIFO behaviour, the push, pop and peek operations with a stack pointer, overflow and underflow, and the use of the call stack.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)