Recall & Review
beginner
What is a nested dictionary in Python?
A nested dictionary is a dictionary where the values can also be dictionaries. It means you have dictionaries inside another dictionary, like boxes inside a bigger box.
Click to reveal answer
beginner
How do you access a value inside a nested dictionary?
You use multiple square brackets, one for each dictionary level. For example,
data['key1']['key2'] accesses the value inside the inner dictionary under 'key2'.Click to reveal answer
beginner
How can you add a new key-value pair inside a nested dictionary?
First access the inner dictionary, then assign a new key and value like
data['outer_key']['new_key'] = value. This adds or updates the inner dictionary.Click to reveal answer
intermediate
What happens if you try to access a key that does not exist in a nested dictionary?
Python raises a
KeyError because the key is missing. To avoid this, you can use methods like .get() or check if the key exists first.Click to reveal answer
intermediate
How can you loop through all keys and values in a nested dictionary?
Use a nested loop: the outer loop goes through the main dictionary, and the inner loop goes through each inner dictionary. This way you can access all keys and values at every level.
Click to reveal answer
What is the correct way to access the value 42 in this nested dictionary?
{'a': {'b': 42}}✗ Incorrect
You first access the outer key 'a', then the inner key 'b' to get 42.
How do you add a new key 'c' with value 100 inside the inner dictionary of {'a': {'b': 42}}?
✗ Incorrect
You access the inner dictionary with data['a'] and assign the new key 'c' with value 100.
What error occurs if you try to access a missing key in a nested dictionary without checking?
✗ Incorrect
Accessing a missing key in a dictionary raises a KeyError.
Which method safely gets a value from a nested dictionary without raising an error if the key is missing?
✗ Incorrect
The .get() method returns None or a default value if the key is missing, avoiding KeyError.
How do you loop through all keys and values in a nested dictionary?
✗ Incorrect
Nested for loops let you access keys and values at each level of the nested dictionary.
Explain what a nested dictionary is and how you access values inside it.
Think about dictionaries inside dictionaries and how you use multiple brackets.
You got /2 concepts.
Describe how to safely handle missing keys when working with nested dictionaries.
Consider what happens if a key is not found and how to avoid errors.
You got /3 concepts.