Practice: Nested Dictionaries
Q1. Read one inner value
Given the dictionary `people = {"amy": {"city": "Pune"}, "bob": {"city": "Delhi"}}`, print the city of bob.
Goal: Read nested values.
Q2. Add a new record
Start with `team = {"a": {"score": 5}}`. Add a record for key `b` with score 7, then print the whole dictionary.
Goal: Add inner dictionaries.
Q3. Update an inner value
Given `item = {"pen": {"stock": 10}}`, reduce the stock of pen by 3 and print it.
Goal: Update nested values.
Q4. Safe lookup
Given `users = {"u1": {"name": "Ria"}}`, print the email of user `u2` or the text `no email` without raising an error.
Goal: Use get() for safe nested reads.
Q5. Total of inner values
Given `sales = {"jan": {"a": 10, "b": 5}, "feb": {"a": 7, "b": 8}}`, print the total of all numbers.
Goal: Loop over nested data.
Q6. Count with setdefault
Given the list of pairs `[("x", "a"), ("x", "b"), ("y", "a")]`, build a nested dictionary that stores for each first item a dictionary of second items with value 1. Print it.
Goal: Build nested data with setdefault.
Q7. What does this print? (chained keys)
Predict the output.
d = {"a": {"b": {"c": 42}}}
print(d["a"]["b"]["c"])Goal: Read deep values.
Q8. What does this print? (inner keys)
Predict the output.
d = {"k1": {"x": 1, "y": 2}, "k2": {"z": 3}}
print(list(d["k1"].keys()))Goal: Use methods on inner dictionaries.
Q9. What does this print? (get default)
Predict the output.
d = {"a": {"n": 1}}
print(d.get("b", {}).get("n", 0))Goal: Safe nested read.
Q10. What does this print? (shallow copy)
Predict the output.
d = {"a": {"n": 1}}
e = d.copy()
e["a"]["n"] = 5
print(d["a"]["n"])Goal: Understand shallow copy.
Q11. What does this print? (deep copy)
Predict the output.
import copy
d = {"a": [1, 2]}
e = copy.deepcopy(d)
e["a"].append(3)
print(d, e)Goal: Use deepcopy.
Q12. What does this print? (loop)
Predict the output.
d = {"a": {"p": 1}, "b": {"p": 2, "q": 3}}
n = 0
for inner in d.values():
n += len(inner)
print(n)Goal: Loop over inner dictionaries.
Q13. Fix the KeyError
This program crashes because `ravi` has no `phone` key. Fix it to print `none`.
data = {"ravi": {"age": 16}}
print(data["ravi"]["phone"])Goal: Avoid KeyError.
Q14. Fix the tuple key bug
The code should print 5 but crashes. Fix it.
d = {"a": {"b": 5}}
print(d["a", "b"])Goal: Use correct chained access.
Q15. Fix the missing inner dictionary
The code should store a score for `amy`, but it crashes. Fix it.
scores = {}
scores["amy"]["math"] = 90
print(scores)Goal: Create inner levels safely.
Q16. Why deep nesting is hard
Give one reason why very deep nested dictionaries are hard to work with.
Goal: Know when to stop nesting.
Q17. Shallow versus deep copy
Explain the difference between `dict.copy()` and `copy.deepcopy()` for a nested dictionary.
Goal: Understand copying.
Q18. Print a student report
Given `marks = {"asha": {"maths": 90, "science": 80}, "ravi": {"maths": 70, "science": 60}}`, print each student's name and average mark in the form `asha 85.0`.
Goal: Apply nested loops.
Q19. Find the best seller
Given `shop = {"pen": {"sold": 30}, "book": {"sold": 45}, "bag": {"sold": 12}}`, print the name of the item with the most sold.
Goal: Combine max with nested data.