0
0
Intro to Computingfundamentals~20 mins

Dictionaries and key-value pairs in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dictionary Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
trace
intermediate
2: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'] = 5
A{'a': 5, 'b': 2, 'c': 7}
B{'a': 1, 'b': 2, 'c': 3}
C{'a': 5, 'b': 2, 'c': 3}
D{'a': 1, 'b': 2, 'c': 7}
Attempts:
2 left
💡 Hint
Remember that dictionary values can be updated after they are used to calculate other values.
🧠 Conceptual
intermediate
1:30remaining
Understanding dictionary keys
Which of the following can be used as a key in a dictionary?
AA string like 'name'
BA list like [1, 2, 3]
CA dictionary like {'x': 1}
DA set like {1, 2, 3}
Attempts:
2 left
💡 Hint
Dictionary keys must be immutable types.
Comparison
advanced
2:00remaining
Compare dictionary creation methods
Which option creates a dictionary with keys 1, 2, 3 and values their squares?
A{x: x**2 for x in [1, 2, 3]}
Bdict((x, x**2) for x in [1, 2, 3])
C{(x, x**2) for x in [1, 2, 3]}
Ddict[x, x**2 for x in [1, 2, 3]]
Attempts:
2 left
💡 Hint
Look for the syntax that correctly creates key-value pairs.
identification
advanced
1: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']
AIndexError
BTypeError
CSyntaxError
DKeyError
Attempts:
2 left
💡 Hint
The key 'z' does not exist in the dictionary.
🚀 Application
expert
2: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}
Alen(set(data.values()))
Blen(data)
Clen(set(data.keys()))
Dlen(data.values())
Attempts:
2 left
💡 Hint
Keys and values are different; unique values means unique dictionary values.