Practice: Nested Dictionaries

« Read the chapter

Q1. Read one inner value

BeginnerCoding

Given the dictionary `people = {"amy": {"city": "Pune"}, "bob": {"city": "Delhi"}}`, print the city of bob.

Goal: Read nested values.

Q2. Add a new record

BeginnerCoding

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

BeginnerCoding

Given `item = {"pen": {"stock": 10}}`, reduce the stock of pen by 3 and print it.

Goal: Update nested values.

Q4. Safe lookup

IntermediateCoding

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

IntermediateCoding

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

AdvancedCoding

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)

BeginnerOutput

Predict the output.

Code
d = {"a": {"b": {"c": 42}}}
print(d["a"]["b"]["c"])

Goal: Read deep values.

Q8. What does this print? (inner keys)

BeginnerOutput

Predict the output.

Code
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)

IntermediateOutput

Predict the output.

Code
d = {"a": {"n": 1}}
print(d.get("b", {}).get("n", 0))

Goal: Safe nested read.

Q10. What does this print? (shallow copy)

IntermediateOutput

Predict the output.

Code
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)

AdvancedOutput

Predict the output.

Code
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)

IntermediateOutput

Predict the output.

Code
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

BeginnerDebugging

This program crashes because `ravi` has no `phone` key. Fix it to print `none`.

Code
data = {"ravi": {"age": 16}}
print(data["ravi"]["phone"])

Goal: Avoid KeyError.

Q14. Fix the tuple key bug

IntermediateDebugging

The code should print 5 but crashes. Fix it.

Code
d = {"a": {"b": 5}}
print(d["a", "b"])

Goal: Use correct chained access.

Q15. Fix the missing inner dictionary

IntermediateDebugging

The code should store a score for `amy`, but it crashes. Fix it.

Code
scores = {}
scores["amy"]["math"] = 90
print(scores)

Goal: Create inner levels safely.

Q16. Why deep nesting is hard

BeginnerConceptual

Give one reason why very deep nested dictionaries are hard to work with.

Goal: Know when to stop nesting.

Q17. Shallow versus deep copy

IntermediateConceptual

Explain the difference between `dict.copy()` and `copy.deepcopy()` for a nested dictionary.

Goal: Understand copying.

Q18. Print a student report

IntermediateApplication

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

AdvancedApplication

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.