Nested Dicts: One Structure, All the Data
The problem...
You have three soldiers. Each one has a name, a score, and a status.
You could store them as three separate dicts:
raven = {"score": 85, "active": True}
wolf = {"score": 74, "active": False}
ghost = {"score": 91, "active": True}
But how do you work with all three together? Loop through them? Pass them to a function?
Three separate variables don't scale. You need one structure that holds all three.
The idea!
A dict can hold any value — including other dicts.
A dict that contains dicts is called a nested dict.
One outer dict. Each key points to an inner dict. All of it in one place.
Making it real
roster = {
"Raven": {"score": 85, "active": True},
"Wolf": {"score": 74, "active": False},
"Ghost": {"score": 91, "active": True}
}
print(roster)
Output → {'Raven': {'score': 85, 'active': True}, 'Wolf': {'score': 74, 'active': False}, 'Ghost': {'score': 91, 'active': True}}
Three soldiers. One structure. Each name is a key. Each value is a full dict.
Accessing elements
Two keys — outer first, inner second:
roster = {
"Raven": {"score": 85, "active": True},
"Wolf": {"score": 74, "active": False}
}
print(roster["Raven"]) # inner dict
print(roster["Raven"]["score"]) # value inside inner dict
Output:
{'score': 85, 'active': True}
85
roster["Raven"] gets you the inner dict. ["score"] gets you the value inside it.
Looping through a nested dict
roster = {
"Raven": {"score": 85, "active": True},
"Wolf": {"score": 74, "active": False},
"Ghost": {"score": 91, "active": True}
}
for name, data in roster.items():
print(f"{name}: score {data['score']}, active {data['active']}")
Output:
Raven: score 85, active True
Wolf: score 74, active False
Ghost: score 91, active True
The outer loop gives you each soldier. Inside the loop, data is the inner dict — treat it like any other dict.
Modifying nested dicts
Reach in with two keys, then assign:
roster = {
"Raven": {"score": 85, "active": True},
"Wolf": {"score": 74, "active": False}
}
roster["Wolf"]["active"] = True
roster["Raven"]["score"] = 91
print(roster["Wolf"])
print(roster["Raven"]["score"])
Output:
{'score': 74, 'active': True}
91
Adding a new entry
roster["Viper"] = {"score": 78, "active": True}
print(roster["Viper"])
Output → {'score': 78, 'active': True}
What's really happening
Each inner dict is a full dict — its own keys, its own values, its own methods.
The outer dict just holds references to them.
When you write roster["Raven"], Python returns the inner dict. Then ["score"] looks up inside that.
Heads up!
- Two keys to access a value — outer first, inner second
- Use
get()on inner dicts too if the key might be missing - Inner dicts don't have to have the same keys — Python doesn't enforce uniformity
- Nesting more than two levels deep gets hard to read fast — keep it shallow
The mindset shift
Stop thinking: "I need a separate dict for each entity."
Start thinking: "Each entity is a key. Its data is the value — another dict."
What you should understand now
- A dict can contain other dicts as values
- Use two keys to access a value — outer key first, inner key second
- Loop with
.items()— the inner dict is just another variable inside the loop - All dict methods work on inner dicts
- Inner dicts don't need to have the same structure