ReviseAlgo Logo

Dictionaries

Nested Dictionaries

Dictionaries within dictionaries for hierarchical data

Interview: Essential for working with JSON, APIs, config files, and complex data structures

Last Updated: June 12, 2026 10 min read

Nested dictionaries are dictionaries that contain other dictionaries as values. They are fundamental for representing hierarchical data structures, JSON-like data, configuration files, and complex records. Mastering nested dict operations is crucial for real-world Python programming.

Creating Nested Dictionaries

There are several ways to build nested dictionaries, from literal syntax to programmatic construction:

  • Literal syntax: Directly embed dicts inside dicts — best for known, static structures
  • Incremental building: Start with an empty dict and add nested dicts as needed
  • Dict comprehension: Build nested structures programmatically from data
  • setdefault(): Safely create nested levels without KeyError — d.setdefault(key, {})
  • defaultdict: Auto-create nested dicts on access — tree = lambda: defaultdict(tree)

Accessing Nested Values

The main challenge with nested dicts is safely accessing deep values when intermediate keys might not exist:

  • Direct chaining: d["a"]["b"]["c"] — raises KeyError if any key is missing
  • Chained .get(): d.get("a", {}).get("b", {}).get("c", default) — safe but verbose
  • Try/except: Wrap access in try/except KeyError for clean error handling
  • Custom helper: Write a deep_get(d, *keys) function for reusable deep access

Pro Tip: Deep Access Helper

Write a reusable function: def deep_get(d, keys, default=None): that walks through keys iteratively, returning default if any key is missing. This avoids ugly chained .get() calls throughout your code.

Iterating Nested Structures

Iterating over nested dicts requires careful handling of the hierarchy levels:

  • Two-level iteration: Nested for loops — outer for parent keys, inner for child keys
  • Recursive traversal: For arbitrarily deep nesting, use recursion to walk the structure
  • Flattening: Convert nested dict to flat dict with composite keys like "parent_child"
  • json_normalize: In pandas, pd.json_normalize() flattens nested JSON data automatically

Modifying and Merging Nested Dicts

  • Direct assignment: d["a"]["b"] = new_value — works if path exists
  • Deep merge: Recursively merge two nested dicts, preserving values at all levels (not just top-level)
  • copy.deepcopy(): Always use deep copy for nested dicts — shallow copy only copies the top level
  • Updating nested values: Navigate to the correct level, then use .update() or direct assignment

Common Pitfall: Shallow vs Deep Copy

Using dict.copy() or dict(d) on a nested dict creates a shallow copy. Modifying nested dicts in the copy will affect the original! Always use copy.deepcopy(d) for nested structures.

Real-World Applications

  • JSON data: APIs return nested JSON that maps directly to nested Python dicts
  • Configuration files: YAML, TOML, and INI files often represent nested structures
  • Tree structures: File systems, organizational charts, category hierarchies
  • Data aggregation: Grouping data by multiple keys (e.g., by year then by category)

Use Cases

Parsing and manipulating JSON responses from REST APIs

Storing hierarchical configuration (app settings, environment configs)

Building tree structures like file systems or organizational charts

Aggregating data by multiple grouping keys

Representing graph adjacency lists and weighted networks

Common Mistakes

Using shallow copy (dict.copy()) instead of copy.deepcopy() — mutations leak to the original

Accessing deep keys without checking if intermediate keys exist — causes KeyError

Not using setdefault() or defaultdict when building nested dicts programmatically

Writing deeply nested for loops instead of recursive functions for arbitrary-depth structures

Forgetting that .update() only merges the top level — use a deep merge function for nested dicts