Challenge - 5 Problems
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ trace
intermediate2:00remaining
Trace the dictionary update
What is the final content of the dictionary
data after running the code below?Intro to Computing
data = {'a': 1, 'b': 2}
data['c'] = data['a'] + data['b']
data['a'] = 5Attempts:
2 left
💡 Hint
Remember that dictionary values can be updated after they are used to calculate other values.
✗ Incorrect
The key 'c' is assigned the sum of values of 'a' and 'b' at that moment, which is 1 + 2 = 3. Then 'a' is updated to 5, but 'c' remains 3.
🧠 Conceptual
intermediate1:30remaining
Understanding dictionary keys
Which of the following can be used as a key in a dictionary?
Attempts:
2 left
💡 Hint
Dictionary keys must be immutable types.
✗ Incorrect
Dictionary keys must be immutable (unchangeable). Strings are immutable, but lists, dictionaries, and sets are mutable and cannot be keys.
❓ Comparison
advanced2:00remaining
Compare dictionary creation methods
Which option creates a dictionary with keys 1, 2, 3 and values their squares?
Attempts:
2 left
💡 Hint
Look for the syntax that correctly creates key-value pairs.
✗ Incorrect
Option B has invalid syntax (missing ':' after key 'x'). Option B uses the dict() constructor with a generator of (key, value) tuples, creating {1: 1, 2: 4, 3: 9}. Option B creates a set of tuples. Option B has invalid syntax.
❓ identification
advanced1:30remaining
Identify the error type
What error will this code raise?
my_dict = {'x': 10, 'y': 20}
value = my_dict['z']Intro to Computing
my_dict = {'x': 10, 'y': 20}
value = my_dict['z']Attempts:
2 left
💡 Hint
The key 'z' does not exist in the dictionary.
✗ Incorrect
Accessing a dictionary with a key that does not exist raises a KeyError.
🚀 Application
expert2:30remaining
Count unique values in a dictionary
Given the dictionary
data = {'a': 1, 'b': 2, 'c': 1, 'd': 3}, which code snippet correctly counts how many unique values are present?Intro to Computing
data = {'a': 1, 'b': 2, 'c': 1, 'd': 3}Attempts:
2 left
💡 Hint
Keys and values are different; unique values means unique dictionary values.
✗ Incorrect
To count unique values, convert the dictionary's values to a set (which removes duplicates) and count its length.