Challenge - 5 Problems
Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this dictionary update?
Consider the following Python code that updates a dictionary. What will be printed?
Python
data = {'a': 1, 'b': 2}
data['c'] = 3
data['a'] = 4
print(data)Attempts:
2 left
๐ก Hint
Remember that assigning a value to an existing key updates it.
โ Incorrect
The key 'c' is added with value 3, and the key 'a' is updated from 1 to 4.
โ Predict Output
intermediate2:00remaining
What does this code print after using update()?
Look at this code snippet. What will be the output after the update method?
Python
info = {'name': 'Alice', 'age': 25}
info.update({'age': 30, 'city': 'NY'})
print(info)Attempts:
2 left
๐ก Hint
The update method changes existing keys and adds new ones.
โ Incorrect
The 'age' key is updated to 30 and 'city' is added.
โ Predict Output
advanced2:30remaining
What is the output when updating nested dictionaries?
What will this code print?
Python
settings = {'display': {'brightness': 70, 'contrast': 50}}
settings['display']['brightness'] = 80
settings['sound'] = {'volume': 40}
print(settings)Attempts:
2 left
๐ก Hint
Updating nested dictionary keys changes the inner dictionary values.
โ Incorrect
The brightness is updated to 80 inside 'display', and 'sound' is added as a new dictionary.
โ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code produce?
Python
d = {'x': 1}
d[2,3] = 5
print(d)Attempts:
2 left
๐ก Hint
Can tuples be used as dictionary keys?
โ Incorrect
Tuples are immutable and can be dictionary keys, so no error occurs.
โ Predict Output
expert3:00remaining
What is the final dictionary after these updates?
What will be printed after running this code?
Python
d = {'a': 1, 'b': 2}
updates = [('a', 3), ('c', 4), ('b', 5)]
for k, v in updates:
d[k] = v
print(d)Attempts:
2 left
๐ก Hint
Each tuple updates or adds a key-value pair in the dictionary.
โ Incorrect
Keys 'a' and 'b' are updated to 3 and 5 respectively, 'c' is added with 4.