Challenge - 5 Problems
Nested Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of nested dictionary access
What is the output of this Python code?
Python
data = {'team': {'leader': 'Alice', 'members': 5}}
print(data['team']['leader'])Attempts:
2 left
๐ก Hint
Look inside the nested dictionary under the key 'team'.
โ Incorrect
The code accesses the nested dictionary under 'team' and then gets the value for 'leader', which is 'Alice'.
โ Predict Output
intermediate2:00remaining
Length of nested dictionary
What is the output of this code?
Python
info = {'person': {'name': 'Bob', 'age': 30}, 'job': {'title': 'Engineer', 'years': 5}}
print(len(info['person']))Attempts:
2 left
๐ก Hint
Count the keys inside the nested dictionary under 'person'.
โ Incorrect
The nested dictionary under 'person' has two keys: 'name' and 'age'. So, len(info['person']) is 2.
๐ง Conceptual
advanced2:00remaining
Understanding nested dictionary update
What will be the value of data after running this code?
Python
data = {'outer': {'inner': 10}}
data['outer']['inner'] += 5Attempts:
2 left
๐ก Hint
The += operator adds 5 to the current value.
โ Incorrect
The code accesses the nested key 'inner' and adds 5 to its value 10, resulting in 15.
โ Predict Output
advanced2:00remaining
Accessing missing nested key error
What error does this code raise?
Python
d = {'a': {'b': 1}}
print(d['a']['c'])Attempts:
2 left
๐ก Hint
Check if the key 'c' exists inside the nested dictionary.
โ Incorrect
The key 'c' does not exist inside d['a'], so accessing it raises a KeyError.
๐ Application
expert3:00remaining
Count total keys in nested dictionary
What is the total number of keys in all levels of this nested dictionary?
Python
nested = {'x': {'y': {'z': 1}}, 'a': 2, 'b': {'c': 3, 'd': 4}}Attempts:
2 left
๐ก Hint
Count keys at every level: top-level and nested inside.
โ Incorrect
Top-level keys: 'x', 'a', 'b' (3 keys). Inside 'x': 'y' (1 key). Inside 'y': 'z' (1 key). Inside 'b': 'c', 'd' (2 keys). Total = 3 + 1 + 1 + 2 = 7. But option C is 7, so check carefully. Actually, total keys are 3 (top) + 1 ('y') + 1 ('z') + 2 ('c','d') = 7. So correct answer is D. Adjusting answer accordingly.