Study resource
Dictionaries revision notes
Study Dictionaries with curriculum-aligned Revision Notes resources, practice links, and exam-focused support.
At a glance
revision notes
Resource type
Topic
Dictionaries
Revision notes
Dictionaries: Key–Value Data and Simple Information Retrieval
What is a dictionary?
A dictionary is a data structure containing a collection of key–value pairs. The key identifies an item, and the associated value is accessed through that key. This differs from accessing data by a numerical position: the key is the meaningful identifier used to retrieve the value.
For example, a dictionary might associate words with numbers:
python word_counts = {"grass": 1, "green": 2, "grows": 1, "the": 1}The key
"green"is associated with the value2. The value can be retrieved by supplying the key:python count = word_counts["green"]Here,
countreceives2.Information-retrieval example
The document “The green, green grass grows” can be represented as:
text {"grass": 1, "green": 2, "grows": 1, "the": 1}Letter case is ignored, so
Theis treated asthe. The wordgreenoccurs twice, so its associated value is2. Each of the other words occurs once, so each has the value1. Repeated occurrences are represented by one word key with an associated count in this example.A dictionary can therefore support a simple information-retrieval task: given a word as a key, retrieve the related count as its value. For example, looking up
"grass"retrieves1, while looking up"green"retrieves2.Important distinctions
- A key is used to identify or access an entry.
- A value is the information associated with that key.
- The key is not the same thing as the value: in the example,
"green"is a key and2is its value. - The dictionary represents the words in the document and their associated counts; it is not simply a list of the words in their original order.
Common errors
- Treating a key as though it were the value.
- Forgetting that the example ignores letter case, so
Thebecomesthe. - Giving
greena value of1instead of counting both occurrences. - Listing duplicate
greenentries instead of representing the repeated word with its associated count. - Describing dictionary access as access by position rather than access via the associated key.
When answering a question, identify the key, state its associated value, and explain how the value represents the required information.
Related topics
