Nested Dictionaries
Nested dictionaries hold complex hierarchical data — perfect for modeling real-world entities like users, products, and API responses.
15 min•By Priygop Team•Updated 2026
Nested Dicts
Nested Dicts
# Nested dictionary
company = {
"name": "TechCorp",
"departments": {
"engineering": {
"head": "Alice",
"team_size": 25,
"projects": ["API", "Frontend", "DevOps"]
},
"marketing": {
"head": "Bob",
"team_size": 15,
"projects": ["Campaign", "SEO"]
}
}
}
# Access nested values
print(company["departments"]["engineering"]["head"]) # Alice
print(company["departments"]["engineering"]["projects"][0]) # API
# Safe nested access
eng = company.get("departments", {}).get("engineering", {})
print(eng.get("team_size", 0)) # 25
# Iterate nested
for dept_name, dept_info in company["departments"].items():
print(f"\n{dept_name.upper()}")
print(f" Head: {dept_info['head']}")
print(f" Team: {dept_info['team_size']}")
print(f" Projects: {', '.join(dept_info['projects'])}")Tip
Tip
Chain .get() calls for safe nested access: data.get('a', {}).get('b', 'default'). This never raises KeyError.
Diagram
Loading diagram…
Map for dictionaries. Object for records/config. WeakMap for metadata that shouldnt prevent GC.
Common Mistake
Warning
data['a']['b']['c'] crashes if any level is missing. Use try/except or chained .get() for nested dicts.
Practice Task
Note
(1) Model a company with departments and employees. (2) Access nested data safely. (3) Iterate and print a formatted report.