Python Nested Dictionaries
In this page:
nested_dict = {
key1: {inner_key: value},
key2: {inner_key: value}
}
nested_dict[key1][inner_key]
Creating a Nested Dictionary
A nested dictionary is written just like a regular dictionary literal, except one or more of its values are themselves dictionary literals -- {"sam": {"age": 28, "city": "Austin"}} represents a single user's data nested one level inside an outer dictionary keyed by username.
Example: Creating a Nested Dictionary
data = {"sam": {"age": 28, "city": "Austin"}}
print(data)
Accessing Nested Values
Reaching a deeply nested value means chaining square-bracket lookups one level at a time -- data["users"]["sam"]["address"]["city"] walks through three levels of nesting to reach the final value, in the exact order the structure is nested.
Example: Accessing Nested Values
data = {"users": {"sam": {"address": {"city": "Austin"}}}}
print(data["users"]["sam"]["address"]["city"])
Modifying Nested Dictionary Values
Updating a value inside a nested dictionary uses the same chained bracket syntax as reading it -- data["users"]["sam"]["age"] = 29 reaches into the nested structure and updates just that one specific value, leaving the rest of the structure untouched.
Example: Modifying Nested Dictionary Values
data = {"users": {"sam": {"age": 28}}}
data["users"]["sam"]["age"] = 29 # chained brackets reach into the nested dict
print(data)
Looping Over a Nested Dictionary
Looping over a nested dictionary typically means an outer loop over the top-level keys, with an inner loop (or direct access) handling each nested dictionary's own keys and values -- letting you process every piece of hierarchical data systematically.
Example: Looping Over a Nested Dictionary
users = {"sam": {"age": 28}, "alex": {"age": 30}}
for name, info in users.items(): # outer loop over each user
print(name, info["age"]) # info is the nested dict for that user
The Shallow Copy Trap with Nested Dictionaries
Copying an outer dictionary with .copy() (or dict()) only copies the top level -- any nested dictionaries inside remain shared references between the original and the copy, meaning a change to a nested value through the copy still affects the original. copy.deepcopy() is needed for a genuinely independent copy.
Example: The Shallow Copy Trap with Nested Dictionaries
import copy
original = {"user": {"age": 28}}
shallow = original.copy()
shallow["user"]["age"] = 99
print(original["user"]["age"]) # also changed
deep = copy.deepcopy(original)
deep["user"]["age"] = 1
print(original["user"]["age"]) # unaffected
- Assuming a nested dictionary is copied independently when you copy the outer one with a shallow copy() -- the inner dictionaries remain shared references, not independent copies.
- Chaining several key lookups (data["users"]["sam"]["address"]) without checking each level actually exists first, risking a KeyError partway through the chain.
- Confusing a nested dictionary's structure with a list of dictionaries -- they solve different problems and are not interchangeable without restructuring.
- A nested dictionary is simply a dictionary whose values are themselves dictionaries, letting you represent hierarchical data naturally.
- Accessing a deeply nested value chains multiple square-bracket lookups: data["outer_key"]["inner_key"].
- Nested dictionaries can be built directly as a literal, or constructed incrementally by assigning dictionary values to keys.
Nested dictionaries are a natural consequence of core dict support, available in every Python version.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: