Skip to content
Computer Science · Grade 9 · Cybersecurity and Digital Safety · Term 3

Dictionaries/Maps and Key-Value Pairs

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

Ontario Curriculum ExpectationsCS.HS.AP.12CS.HS.CT.13

About This Topic

Dictionaries, or maps, store data as key-value pairs, where unique keys like strings or integers map to values such as numbers or other data types. Students learn to create, access, update, and delete entries, for example, using a dictionary to hold usernames and encrypted passwords in a cybersecurity context. This approach contrasts with lists, which require indexing or searching, and directly supports the unit on cybersecurity and digital safety by modeling secure data handling.

In the Ontario Grade 9 Computer Science curriculum, this topic advances abstraction and data structures under standards like CS.HS.AP.12 and CS.HS.CT.13. Students explain why key-value pairs offer faster retrieval through hashing compared to linear searches in lists. They design programs to represent real-world relationships, such as access logs or threat databases, fostering computational thinking and efficiency analysis.

Active learning benefits this topic because students code and test dictionaries in real time using IDEs or online repls. Pair challenges to simulate logins or compare lookup speeds make efficiency tangible. Collaborative modeling of cybersecurity scenarios connects abstract syntax to practical digital safety, building confidence and retention through immediate feedback.

Key Questions

  1. Explain the advantages of using key-value pairs for data storage over indexed lists.
  2. Design a program that uses a dictionary to model real-world relationships.
  3. Compare the efficiency of data retrieval in lists versus dictionaries for specific scenarios.

Learning Objectives

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

Before You Start

Introduction to Variables and Data Types

Why: Students need to understand how to store and manipulate basic data like strings and numbers before working with more complex structures.

Lists and Indexed Data Structures

Why: Understanding how lists store data sequentially by index is crucial for comparing their limitations to the key-based access of dictionaries.

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.

Watch Out for These Misconceptions

Common MisconceptionDictionaries work exactly like lists with numeric indices.

What to Teach Instead

Keys can be strings, tuples, or other immutable types, enabling direct access without position knowledge. Hands-on pair programming with string-key dictionaries helps students experience flexible lookups and contrast them with list searches.

Common MisconceptionData retrieval speed is the same in lists and dictionaries.

What to Teach Instead

Dictionaries use hashing for average O(1) access, while lists need O(n) scans. Whole-class timing challenges reveal this difference empirically, helping students internalize efficiency through observable data.

Common MisconceptionDictionaries maintain insertion order like lists.

What to Teach Instead

In Python 3.7+, dicts preserve order, but access relies on keys, not indices. Small-group login simulations emphasize key-based retrieval over sequence, clarifying structure via practical use.

Active Learning Ideas

See all activities

Real-World Connections

  • Websites use dictionaries to store user profiles, mapping usernames (keys) to their associated data like email addresses and preferences (values). This allows for quick retrieval when a user logs in.
  • Network administrators use dictionaries to track IP addresses (keys) and the devices they are assigned to (values), enabling rapid identification of network traffic sources.
  • Online dictionaries and translation services employ key-value pairs, where a word or phrase (key) is mapped to its definition or translation (value) for fast lookups.

Assessment Ideas

Quick Check

Present 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.'

Discussion Prompt

Pose 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?'

Exit Ticket

Ask 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'.

Frequently Asked Questions

What are advantages of key-value pairs over indexed lists?
Key-value pairs enable direct, constant-time retrieval via hashing, unlike lists that require linear searches scaling poorly with data size. In cybersecurity, this models fast credential checks without scanning entire user lists. Students grasp this by coding comparisons, seeing dicts handle thousands of entries efficiently while lists slow down.
How to model real-world relationships with dictionaries?
Use keys for identifiers like product IDs or usernames, values for details like prices or roles. For digital safety, map threat types to mitigation steps. Programs students design, such as access control systems, reinforce this by simulating scenarios where quick lookups prevent breaches.
How can active learning help teach dictionaries?
Active approaches like pair programming login simulators or class lookup races provide instant feedback on code execution. Students discover hashing speedups through timed tests and collaborate on real-world models, making abstract efficiency concrete. This builds deeper understanding and ties concepts to cybersecurity applications.
Why use dictionaries in cybersecurity programs?
They store sensitive data like user credentials efficiently and securely, allowing O(1) lookups for authentication. Compared to lists, dicts reduce processing time in large datasets. Grade 9 activities modeling threat logs help students see how this supports safe digital practices.