What if you could find any piece of information instantly, no matter how big your data is?
Why Dictionaries and key-value pairs in Intro to Computing? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big phone book where you want to find a friend's phone number. Without any order, you have to flip through every page until you find their name.
This manual search is slow and tiring. You might miss the name or get the wrong number. It's easy to make mistakes and waste time.
Dictionaries store data as pairs: a key (like a name) and a value (like a phone number). This lets you find any value quickly by looking up its key, just like using an index.
names = ['Alice', 'Bob', 'Carol'] numbers = ['123', '456', '789'] # To find Bob's number, search names list for 'Bob' and get number at same position
contacts = {'Alice': '123', 'Bob': '456', 'Carol': '789'}
# To find Bob's number, just do contacts['Bob']It makes finding, adding, or changing information fast and easy, even with lots of data.
Online stores use dictionaries to quickly find product prices by product ID, so you get instant results when shopping.
Dictionaries pair keys with values for quick lookups.
They save time and reduce errors compared to searching lists.
They help organize data like contacts, settings, or inventory efficiently.
Practice
Solution
Step 1: Understand the definition of a dictionary
A dictionary stores data as pairs where each key is linked to a value.Step 2: Compare with other data types
Unlike lists or strings, dictionaries use keys to quickly find values.Final Answer:
A collection of key-value pairs for storing data -> Option DQuick Check:
Dictionary = key-value pairs [OK]
- Confusing dictionary with list or string
- Thinking dictionary is a type of loop
- Believing dictionary stores only numbers
Solution
Step 1: Identify correct dictionary syntax
Dictionaries use curly braces {} with key:value pairs separated by commas.Step 2: Check each option
{'name': 'Alice', 'age': 30}uses correct syntax with colons and curly braces; others use wrong symbols or brackets.Final Answer:
{'name': 'Alice', 'age': 30} -> Option BQuick Check:
Dictionary syntax = curly braces + colons [OK]
- Using square brackets instead of curly braces
- Using '=' or '->' instead of ':'
- Using parentheses instead of braces
person = {'name': 'Bob', 'age': 25}
print(person['age'])Solution
Step 1: Understand dictionary access by key
Accessingperson['age']retrieves the value linked to key 'age'.Step 2: Identify the value for key 'age'
The value stored is 25, soprint(person['age'])outputs 25.Final Answer:
25 -> Option CQuick Check:
person['age'] = 25 [OK]
- Printing the key name instead of value
- Confusing key with value
- Expecting the whole dictionary to print
data = {'x': 10, 'y': 20, 'x': 30}
print(data['x'])Solution
Step 1: Check for duplicate keys in dictionary
The key 'x' appears twice; dictionaries keep only the last value for duplicates.Step 2: Understand output of print statement
Printingdata['x']shows 30, the last assigned value, overwriting 10.Final Answer:
Duplicate keys cause the last value to overwrite earlier ones -> Option AQuick Check:
Duplicate keys overwrite previous values [OK]
- Thinking duplicate keys cause syntax error
- Expecting both values to be stored
- Believing keys must be numbers
students = [('Anna', 85), ('Ben', 92), ('Cara', 85), ('Dan', 92)]Which dictionary comprehension creates a dictionary mapping each student to their score?
Solution
Step 1: Understand the goal
Create a dictionary with student names as keys and scores as values.Step 2: Analyze each comprehension
{name: score for name, score in students}correctly mapsname: score.{score: name for name, score in students}reverses keys and values, causing score keys to overwrite. Options C and D add conditions changing values or keys incorrectly.Final Answer:
{name: score for name, score in students} -> Option AQuick Check:
Keys = names, Values = scores [OK]
- Using scores as keys causing overwrites
- Adding conditions that change keys incorrectly
- Confusing key-value order in comprehension
