How to Get a Value from a Nested Dictionary in Python
Data from JSON files and web APIs usually arrives as dictionaries inside dictionaries: an order holds a customer, who holds an address. To read a value deep inside, you chain lookups, one per level. This guide reads a city out of a nested order on Python 3.15.2, and handles a level that isn't there.
Syntax
<dictionary>[<key 1>][<key 2>]
<dictionary>.get(<key 1>, {}).get(<key 2>, <default>)
| Part | What it means |
|---|---|
<dictionary>[<key 1>] | The value at the first level, which is itself a dictionary |
[<key 2>] | A lookup in that inner dictionary. Add one pair of brackets per level |
.get(<key 1>, {}) | The inner dictionary, or an empty one when the key is missing, so the next .get() still works |
.get(<key 2>, <default>) | The value, or <default> when it's missing |
The built-in types documentation says d[key] "Raises a KeyError if key is not in the map", and that get() returns "the value for key if key is in the dictionary, else default".
The Example
An order is a dictionary with an id and a customer. The customer is a dictionary with a name and an address, which is a dictionary with a city. The script prints the name, the city, and a shipping method that the order doesn't have.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
order = {
"id": 1042,
"customer": {"name": "Ana", "address": {"city": "Lisbon"}},
}
print(order["customer"]["name"])
print(order["customer"]["address"]["city"])
print(order.get("shipping", {}).get("method", "standard"))

order["customer"]is the customer dictionary, soorder["customer"]["name"]is the name inside it.- The city is three levels down, so it takes three pairs of brackets.
- The order has no
shippingkey.order.get("shipping", {})returns an empty dictionary, and its.get("method", "standard")returns the default.
Step 2: Run It
uv run python nested_dict.py
Ana
Lisbon
standard

Each pair of brackets went one level deeper, and the chained .get() calls returned the default for the missing shipping method instead of stopping the script. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using brackets for a level that may be missing: order["shipping"]["method"] | KeyError: 'shipping' | Use .get("shipping", {}) for that level |
Leaving out the {} default: order.get("shipping").get("method") | AttributeError: 'NoneType' object has no attribute 'get' | Give the first .get() an empty dictionary as its default |
Writing the path as one key: order["customer.name"] | KeyError: 'customer.name': Python looks for a key with a dot in its name | One pair of brackets per level: order["customer"]["name"] |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
KeyError: '...' | That key is missing at its level, or spelled differently | Print the level above, such as print(order["customer"]), to see its keys |
AttributeError: 'NoneType' object has no attribute 'get' | A .get() without a default returned None | Pass {} as the default for every level except the last |
TypeError: string indices must be integers, not 'str' | A level you expected to be a dictionary is text | Check the data's shape with print(type(...)) |
FAQ
Brackets or .get()?
Use brackets for keys that must be there: a KeyError then tells you the data is wrong. Use chained .get() for parts that are allowed to be missing.
How deep can a dictionary be nested?
As deep as the data goes. Each level adds one more pair of brackets, or one more .get().
Conclusion
order["customer"]["address"]["city"] read the city three levels down, and order.get("shipping", {}).get("method", "standard") returned a default for a level that didn't exist. Use brackets where a key must exist and .get() with {} where a level may be missing.
Additional Resources
- Mapping types —
d[key],get()and the other dictionary operations. - Dictionaries — the tutorial.
