Skip to content

Dictionaries/Maps and Key-Value PairsActivities & Teaching Strategies

Active learning works for dictionaries and maps because key-value pairs require students to move beyond abstract definitions and engage with concrete operations like insertion, retrieval, and comparison. By manipulating data directly in varied contexts, students build intuitive understanding of how keys enable faster, more flexible access than lists or indexes alone.

Grade 9Computer Science4 activities20 min40 min

Learning Objectives

  1. 1Compare the efficiency of data retrieval between indexed lists and dictionaries for large datasets.
  2. 2Design a Python program that uses a dictionary to manage user credentials and access permissions.
  3. 3Explain how hash functions contribute to the speed of key-value pair lookups in dictionaries.
  4. 4Create a dictionary to model relationships between cybersecurity threats and their mitigation strategies.

Want a complete lesson plan with these objectives? Generate a Mission

Pair Programming: Contact Database

Pairs create a dictionary with names as keys and emails as values. They add three entries, retrieve specific values, and handle missing keys with error checks. Pairs then modify one value and print the updated dictionary.

Prepare & details

Explain the advantages of using key-value pairs for data storage over indexed lists.

Facilitation Tip: During Pair Programming: Contact Database, circulate to ensure both partners alternate roles between typing and reviewing code.

Setup: Groups at tables with problem materials

Materials: Problem packet, Role cards (facilitator, recorder, timekeeper, reporter), Problem-solving protocol sheet, Solution evaluation rubric

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
40 min·Small Groups

Small Groups: Secure Login Checker

Groups build a username-password dictionary and write a function to simulate login: input credentials, check against dict, output success or failure. Add a feature to lock accounts after three failed attempts. Test with group-generated data.

Prepare & details

Design a program that uses a dictionary to model real-world relationships.

Facilitation Tip: For the Secure Login Checker small groups, provide a partially completed sample dictionary so students focus on logic rather than syntax.

Setup: Groups at tables with problem materials

Materials: Problem packet, Role cards (facilitator, recorder, timekeeper, reporter), Problem-solving protocol sheet, Solution evaluation rubric

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
20 min·Whole Class

Whole Class: Lookup Speed Challenge

Display code snippets using lists and dictionaries with 100 sample entries. Class runs timed lookups for random keys, records average times, and discusses hashing advantages. Volunteers share results on shared screen.

Prepare & details

Compare the efficiency of data retrieval in lists versus dictionaries for specific scenarios.

Facilitation Tip: In the Lookup Speed Challenge, reset timers between trials to prevent students from optimizing based on prior runs.

Setup: Groups at tables with problem materials

Materials: Problem packet, Role cards (facilitator, recorder, timekeeper, reporter), Problem-solving protocol sheet, Solution evaluation rubric

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
30 min·Individual

Individual: Real-World Modeler

Students design a dictionary-based program for a school inventory: items as keys, quantities as values. Include functions to add stock, check availability, and generate reports. Submit code with test cases.

Prepare & details

Explain the advantages of using key-value pairs for data storage over indexed lists.

Facilitation Tip: During Individual: Real-World Modeler, offer starter code with comments to reduce cognitive load on data structure setup.

Setup: Groups at tables with problem materials

Materials: Problem packet, Role cards (facilitator, recorder, timekeeper, reporter), Problem-solving protocol sheet, Solution evaluation rubric

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management

Teaching This Topic

Teach dictionaries by starting with a relatable scenario, such as storing usernames and passwords, to ground the concept in a real-world need. Avoid introducing hashing mechanics early; instead, let students observe performance differences through timing challenges. Emphasize that keys are unique identifiers, not positions, and model this by comparing dictionary access with list searches side by side.

What to Expect

Students should confidently create dictionaries, explain why a key-value structure fits a problem, and perform lookups or updates without confusion. They should also recognize when a dictionary improves efficiency over a list, especially in retrieval tasks like login systems.

These activities are a starting point. A full mission is the experience.

  • Complete facilitation script with teacher dialogue
  • Printable student materials, ready for class
  • Differentiation strategies for every learner
Generate a Mission

Watch Out for These Misconceptions

Common MisconceptionDuring Pair Programming: Contact Database, watch for students treating dictionary keys like list indices by trying to access data with positions such as user[0] instead of user['name'].

What to Teach Instead

Prompt students to print their dictionary and observe the actual keys. Ask them to trace the error message together to identify the mismatch between their code and the dictionary structure.

Common MisconceptionDuring Lookup Speed Challenge, watch for students assuming dictionaries and lists have similar access speeds because both complete tasks quickly on small datasets.

What to Teach Instead

Have students increase the dataset size tenfold and rerun the challenge. Ask them to graph completion times and compare the steepness of the curves to highlight the logarithmic or linear difference.

Common MisconceptionDuring Secure Login Checker small groups, watch for students retrieving data using index positions like log[1] instead of keys like log['username'].

What to Teach Instead

Circulate with a sticky note that shows the correct retrieval line and ask each group to explain why their current method fails to match the key-value structure they designed.

Assessment Ideas

Quick Check

After Pair Programming: Contact Database, ask students to explain in one sentence why they chose a dictionary over a list to store contacts, referencing keys and values in their response.

Discussion Prompt

After Secure Login Checker small groups, prompt students to share how their dictionary improved the efficiency of searching for specific login attempts compared to a list, using their timing data as evidence.

Exit Ticket

During Lookup Speed Challenge, collect students' recorded times and ask them to write a one-line reflection: 'One thing I learned about dictionary access speed is...' and submit it before leaving.

Extensions & Scaffolding

  • Challenge students to implement a login system that logs failed attempts and locks accounts after three incorrect passwords, using nested dictionaries.
  • For students who struggle, provide a scaffolded worksheet with blanks for keys and values, and ask them to match elements from a word bank.
  • Deeper exploration: Introduce students to default dictionaries or the collections module to handle missing keys gracefully in their login checker.

Key Vocabulary

Dictionary (Map)A data structure that stores data in key-value pairs. Each key must be unique and maps to a specific value.
Key-Value PairA fundamental unit in a dictionary, consisting of a unique identifier (the key) and its associated data (the value).
Hash FunctionAn algorithm that takes an input (the key) and returns a fixed-size string of bytes, typically a number. This helps in quickly locating the value associated with a key.
CollisionWhen two different keys produce the same hash value, requiring a strategy to handle the situation and store both values correctly.

Ready to teach Dictionaries/Maps and Key-Value Pairs?

Generate a full mission with everything you need

Generate a Mission