Challenge - 5 Problems
Key Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of accessing a nested dictionary value?
Given the dictionary
data = {'user': {'name': 'Alice', 'age': 30}}, what is the output of print(data['user']['name'])?Python
data = {'user': {'name': 'Alice', 'age': 30}}
print(data['user']['name'])Attempts:
2 left
๐ก Hint
Think about how to access values inside nested dictionaries using keys.
โ Incorrect
The key 'user' gives the nested dictionary {'name': 'Alice', 'age': 30}. Then accessing 'name' inside it returns 'Alice'.
โ Predict Output
intermediate2:00remaining
What happens when accessing a missing key directly?
What error will this code produce?
info = {'city': 'Paris', 'country': 'France'}
print(info['population'])Python
info = {'city': 'Paris', 'country': 'France'}
print(info['population'])Attempts:
2 left
๐ก Hint
What happens if you try to get a key that does not exist in a dictionary?
โ Incorrect
Accessing a key that is not present in the dictionary directly raises a KeyError.
โ Predict Output
advanced2:00remaining
What is the output when using the get() method with a default value?
Consider the dictionary
settings = {'theme': 'dark', 'font': 'Arial'}. What does print(settings.get('color', 'blue')) output?Python
settings = {'theme': 'dark', 'font': 'Arial'}
print(settings.get('color', 'blue'))Attempts:
2 left
๐ก Hint
The get() method returns the default value if the key is missing.
โ Incorrect
Since 'color' is not a key in settings, get() returns the default 'blue'.
โ Predict Output
advanced2:00remaining
What is the output when accessing a key with a mutable object as key?
What happens when running this code?
my_dict = {[1, 2]: 'value'}Python
my_dict = {[1, 2]: 'value'}Attempts:
2 left
๐ก Hint
Dictionary keys must be immutable types.
โ Incorrect
Lists are mutable and cannot be used as dictionary keys, so this raises a TypeError.
๐ง Conceptual
expert2:00remaining
How many items are in the dictionary after this code runs?
What is the number of items in
result after executing this code?
keys = ['a', 'b', 'a', 'c']
values = [1, 2, 3, 4]
result = {k: v for k, v in zip(keys, values)}Attempts:
2 left
๐ก Hint
Dictionary keys must be unique; later values overwrite earlier ones.
โ Incorrect
The key 'a' appears twice; the last value (3) overwrites the first (1). So keys are 'a', 'b', 'c' โ 3 items.