A dictionary's values can be lists or dictionaries themselves. book["Riya"]["address"] reads from the outside in: Riya's record first, then the address inside it. record = book["Riya"] isn't a copy. It's a second label on the same inner dictionary, so changing record changes the book.
Try it
main.py
Output
Press Run and what your program prints shows up here.
Easy to get wrong
Thinks record = book[name] makes a copy of the inner dictionary, so changing record leaves the book alone.
Thinks keys of an inner dictionary can be reached straight from the outer one, as book["address"].
Nested data is a Python dictionary or list whose values are themselves dictionaries or lists: a dictionary of customer records, a list of orders, a dictionary that maps each team to a list of names. Most real data has this shape, because each record has several parts and there are many records. JSON from a web service and rows read from a file usually arrive as a list of dicts or a dict of dicts. You reach inside with one pair of square brackets per level, read from the outside in.
How it works
Each pair of brackets works on whatever the part to its left produced. A list takes a position number, a dictionary takes a key, and you can mix them in any order the data needs:
deliveries = [
{"name": "Emma", "parcels": 2},
{"name": "Ivo", "parcels": 5},
]
teams = {"Red": ["Sam", "Ivo"], "Blue": ["Lena"]}
print(deliveries[1]["name"])
print(teams["Red"][0])
teams["Blue"].append("Omar")
print(teams)
for delivery in deliveries:
print(delivery["name"], delivery["parcels"])
Ivo
Sam
{'Red': ['Sam', 'Ivo'], 'Blue': ['Lena', 'Omar']}
Emma 2
Ivo 5
To read deliveries[1]["name"], work left to right: deliveries[1] is Ivo's dictionary, and ["name"] then looks inside that. teams["Blue"] is a list, so .append() adds to it in place, and the change shows when you print teams. Looping over a list of dicts hands you one whole dictionary per turn.
Getting an inner dictionary or list out never copies it. The name you put it in is a second label on the same object, so changing it through that name changes the outer data too. To get a separate one you have to ask for a copy, and how deep the copy goes matters:
import copy
teams = {"Red": ["Sam", "Ivo"]}
shallow = teams.copy()
deep = copy.deepcopy(teams)
teams["Red"].append("Omar")
print(shallow["Red"])
print(deep["Red"])
['Sam', 'Ivo', 'Omar']
['Sam', 'Ivo']
.copy() makes a new outer dictionary, but its values are the same inner lists as before, so the shallow copy sees Omar arrive. copy.deepcopy() copies every level, so nothing done to the original reaches it.
Common mistakes
Expecting record = book["Emma"] to make a copy. It doesn't: record and book["Emma"] are two names for one dictionary, and record["parcels"] = 3 changes the book. Often that is what you want. If it isn't, use book["Emma"].copy(), or copy.deepcopy() when the record holds lists or dictionaries of its own.
Reaching an inner key from the outside. With book = {"Emma": {"address": "Mill Lane"}}, the outer dictionary has one key, "Emma". book["address"] is a KeyError: 'address'. Name every level on the way down: book["Emma"]["address"].
Replacing a record when you meant to change one field.book["Ivo"] = book["Ivo"]["parcels"] + 1 swaps Ivo's whole record for a number. Put the full path on the left as well: book["Ivo"]["parcels"] = book["Ivo"]["parcels"] + 1.