Skip to content
Computing · Year 9 · Advanced Programming with Python · Autumn Term

Dictionaries: Key-Value Pairs

Students will learn to use dictionaries to store and retrieve data using key-value pairs.

National Curriculum Attainment TargetsKS3: Computing - Programming and DevelopmentKS3: Computing - Data Representation

About This Topic

Dictionaries in Python store data as key-value pairs, allowing quick retrieval using unique keys rather than indexes. Year 9 students create dictionaries to manage student records or contact information, using syntax like student['ID'] = 'Grade A'. They add entries with dict[key] = value, retrieve with dict[key], update or delete with del dict[key], and iterate over items with for loops.

This topic supports KS3 Computing standards in programming and data representation. Students compare dictionaries to lists, recognising advantages for lookup-heavy tasks, such as finding a student's grade by ID without scanning entire lists. They design programs and evaluate scenarios, developing skills in selecting appropriate data structures and efficient coding practices.

Active learning suits this topic well. When students code real applications, like a contact book or grade tracker, they test retrieval speeds and handle errors firsthand. Collaborative debugging and timed challenges reveal dictionary strengths, making concepts stick through practical problem-solving.

Key Questions

  1. Compare the advantages of using a dictionary over a list for storing student records.
  2. Design a Python program that uses a dictionary to store contact information.
  3. Evaluate scenarios where a dictionary is the most appropriate data structure.

Learning Objectives

  • Compare the efficiency of data retrieval between Python lists and dictionaries for a dataset of 100 student records.
  • Design a Python program that utilizes a dictionary to store and manage at least five attributes for 10 different contacts.
  • Evaluate three distinct scenarios and justify the selection of a dictionary as the most appropriate data structure for each.
  • Create a Python script that demonstrates adding, accessing, updating, and deleting key-value pairs in a dictionary.

Before You Start

Introduction to Python Lists

Why: Students need to understand how lists store ordered collections of items using numerical indices before comparing them to dictionaries.

Basic Python Syntax and Variables

Why: Familiarity with assigning values to variables and basic data types is essential for understanding key-value assignments.

Key Vocabulary

DictionaryA Python data structure that stores data in unordered collections of key-value pairs. Each key must be unique and immutable.
Key-Value PairA fundamental unit within a dictionary, consisting of a unique identifier (the key) and its associated data (the value).
KeyA unique identifier used to access a specific value within a dictionary. Keys are typically strings or numbers.
ValueThe data associated with a specific key in a dictionary. Values can be of any data type, including lists or other dictionaries.
Hash TableThe underlying data structure often used to implement dictionaries, enabling fast lookups, insertions, and deletions based on keys.

Watch Out for These Misconceptions

Common MisconceptionDictionaries are ordered like lists and can be accessed by index.

What to Teach Instead

Dictionaries rely on keys for access, not position. Pair programming activities where students try list-style indexing lead to KeyError, prompting them to rewrite with proper keys and grasp the difference.

Common MisconceptionDuplicate keys create multiple entries.

What to Teach Instead

New values overwrite existing keys. Small group challenges with deliberate duplicates show this behaviour during testing, helping students verify uniqueness through print statements and iteration.

Common MisconceptionKeys must all be the same data type.

What to Teach Instead

Keys must be immutable and hashable, like strings or integers, but can mix types. Individual debugging tasks expose TypeError on unhashable keys, guiding students to experiment safely.

Active Learning Ideas

See all activities

Real-World Connections

  • Software developers at companies like Google use dictionaries extensively to build features like autocomplete suggestions in search engines, where each typed character (key) maps to a list of possible search queries (values).
  • Financial analysts use dictionaries to store and quickly access stock market data, mapping company ticker symbols (keys) to their current prices, trading volumes, and historical performance data (values).
  • Database administrators employ dictionary-like structures to manage user accounts, mapping usernames (keys) to their permissions, passwords, and profile information (values) for efficient access control.

Assessment Ideas

Quick Check

Present students with a Python code snippet that attempts to access a non-existent key in a dictionary. Ask: 'What error will this code produce, and why? How could you modify the code to handle this situation gracefully?'

Discussion Prompt

Pose the question: 'Imagine you are building a simple inventory system for a small shop. Would you use a list or a dictionary to store the items, and why? Consider how you would add new items, check stock levels, and find a specific product.'

Exit Ticket

Give each student a small card. Ask them to write a Python dictionary representing three fruits, with the fruit name as the key and its color as the value. Then, ask them to write one line of code to retrieve the color of 'apple'.

Frequently Asked Questions

What are the advantages of Python dictionaries over lists for Year 9 students?
Dictionaries offer O(1) average lookup time using keys, ideal for records like student IDs or contacts, unlike lists' O(n) scans. Students see this in timed comparisons, building intuition for efficient data handling in programs.
How to teach key-value pairs in Python dictionaries KS3?
Start with real-world analogies like phone books, then code simple dicts for contacts. Progress to methods like get(), keys(), and loops. Use challenges comparing to lists to highlight lookup speed and flexibility.
Python dictionary examples for student records Year 9?
Example: students = {'001': 'A', '002': 'B'}. Add: students['003'] = 'C'; retrieve: print(students['001']); delete: del students['002']. Extend to nested dicts for full profiles, practicing in programs.
How can active learning help teach Python dictionaries?
Active coding in pairs or groups lets students build, test, and compare dictionaries hands-on, experiencing fast lookups and error handling directly. Collaborative challenges, like racing list vs dict searches, reveal advantages through data, while debugging fosters resilience and deeper grasp of key-value logic over passive explanation.