Skip to content
Computer Science · Grade 9

Active learning ideas

Dictionaries/Maps and Key-Value Pairs

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.

Ontario Curriculum ExpectationsCS.HS.AP.12CS.HS.CT.13
20–40 minPairs → Whole Class4 activities

Activity 01

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.

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

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

What to look forPresent students with a scenario: 'You need to store student names and their final grades. Would you use a list or a dictionary? Explain your choice, referencing keys and values.'

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 02

Collaborative Problem-Solving40 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.

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

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

What to look forPose the question: 'Imagine a cybersecurity system that logs every login attempt, including username and timestamp. How would a dictionary improve the efficiency of searching for all login attempts by a specific user compared to a simple list of all logs?'

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 03

Collaborative Problem-Solving20 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.

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

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

What to look forAsk students to write a short Python code snippet that creates a dictionary to store three common cybersecurity threats (e.g., 'phishing', 'malware', 'DDoS') as keys and a brief description of each as their values. Then, have them write one line of code to retrieve the description for 'malware'.

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 04

Collaborative Problem-Solving30 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.

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

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

What to look forPresent students with a scenario: 'You need to store student names and their final grades. Would you use a list or a dictionary? Explain your choice, referencing keys and values.'

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

A few notes on teaching this unit

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.

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.


Watch Out for These Misconceptions

  • During 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'].

    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.

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

    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.

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

    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.


Methods used in this brief