Working with Nested Dictionaries in Python: Exploring Hierarchical Structures for Data Science 2026

Datatypes Mar 19, 2026

Working with Nested Dictionaries in Python: Exploring Hierarchical Structures for Data Science 2026

Nested dictionaries (dictionaries inside dictionaries) are one of the most powerful and commonly used data structures in modern data science. They naturally represent hierarchical data such as model configurations, JSON API responses, grouped summary statistics, feature metadata, and tree-like structures. Mastering nested dicts lets you handle complex, real-world data with clean, readable, and efficient code.

TL;DR — Essential Techniques

  • Create nested dicts with literals, comprehensions, or defaultdict
  • Access safely with a helper function or .get() chaining
  • Modify and loop through hierarchies with recursion or .items()
  • Use frozendict or types.MappingProxyType for immutable nested configs

1. Creating Nested Dictionaries

# 1. Literal nested dict
model_config = {
    "model_type": "random_forest",
    "params": {
        "n_estimators": 200,
        "max_depth": 10,
        "random_state": 42
    },
    "metadata": {
        "version": "2026.1",
        "author": "Data Team"
    }
}

# 2. Dict comprehension for hierarchical data
import pandas as pd

# Small inline sample so this example runs as-is.
# In a real project you would load it: df = pd.read_csv("sales_data.csv")
df = pd.DataFrame({
    "customer_id": [101, 102, 103, 104, 105, 106],
    "customer_name": ["Ana", "Ben", "Chen", "Dana", "Eli", "Fay"],
    "order_date": ["2026-01-05", "2026-01-12", "2026-02-03",
                   "2026-02-18", "2026-03-01", "2026-03-09"],
    "amount": [1250.75, 890.50, 2340.00, 1780.25, 2615.40, 450.00],
    "quantity": [3, 2, 5, 4, 6, 1],
    "profit": [312.69, 222.63, 585.00, 445.06, 653.85, 112.50],
    "region": ["North", "South", "East", "North", "West", "South"],
    "category": ["Electronics", "Books", "Electronics", "Home", "Home", "Books"],
    "rating": [5, 4, None, 3, 5, 4],
})

summary = {
    region: {
        "total_sales": group["amount"].sum(),
        "avg_amount": round(group["amount"].mean(), 2),
        "count": len(group)
    }
    for region, group in df.groupby("region")
}

2. Safe Access to Nested Data

def safe_get(d, *keys, default=None):
    """Safely navigate nested dictionaries."""
    for key in keys:
        if isinstance(d, dict):
            d = d.get(key, default)
        else:
            return default
    return d

# Usage
n_est = safe_get(model_config, "params", "n_estimators", default=100)
author = safe_get(model_config, "metadata", "author", default="Unknown")

3. Real-World Data Science Examples

import pandas as pd
from collections import defaultdict

# Small inline sample so this example runs as-is.
# In a real project you would load it: df = pd.read_csv("sales_data.csv")
df = pd.DataFrame({
    "customer_id": [101, 102, 103, 104, 105, 106],
    "customer_name": ["Ana", "Ben", "Chen", "Dana", "Eli", "Fay"],
    "order_date": ["2026-01-05", "2026-01-12", "2026-02-03",
                   "2026-02-18", "2026-03-01", "2026-03-09"],
    "amount": [1250.75, 890.50, 2340.00, 1780.25, 2615.40, 450.00],
    "quantity": [3, 2, 5, 4, 6, 1],
    "profit": [312.69, 222.63, 585.00, 445.06, 653.85, 112.50],
    "region": ["North", "South", "East", "North", "West", "South"],
    "category": ["Electronics", "Books", "Electronics", "Home", "Home", "Books"],
    "rating": [5, 4, None, 3, 5, 4],
})

# Example 1: Hierarchical summary by region and category
hierarchical_summary = defaultdict(lambda: defaultdict(dict))

for row in df.itertuples():
    hierarchical_summary[row.region][row.category] = {
        "total": hierarchical_summary[row.region][row.category].get("total", 0) + row.amount,
        "count": hierarchical_summary[row.region][row.category].get("count", 0) + 1
    }

# Example 2: Model hyperparameter grid as nested dict
grid = {
    "random_forest": {
        "n_estimators": [100, 200, 300],
        "max_depth": [None, 10, 20]
    },
    "xgboost": {
        "n_estimators": [100, 200],
        "learning_rate": [0.01, 0.1, 0.3]
    }
}

4. Looping Through Nested Structures

# Recursive or flat iteration
for region, categories in hierarchical_summary.items():
    for category, stats in categories.items():
        print(f"{region} - {category}: total=${stats['total']:,.2f}, count={stats['count']}")

5. Best Practices in 2026

  • Use a safe_get helper for all deep nested access
  • Prefer defaultdict for building hierarchical data dynamically
  • Keep configuration and metadata as nested dicts for clarity
  • Use frozendict or MappingProxyType for immutable nested configs
  • Consider Pydantic or dataclasses for complex nested structures in production

Thinking in Paths

A nested dictionary is easiest to reason about as a tree, where every value is reached by a path of keys such as ("params", "n_estimators"). Reading is safe when every step exists; the problems come from missing intermediate levels. Writing has the opposite problem: you must create each level before assigning a leaf. A small pair of helpers — one to read a path, one to flatten the tree — covers most real use cases, from JSON API responses to configuration files.

Copying needs care too. dict.copy() is shallow: the outer dict is new, but inner dicts are still shared. Use copy.deepcopy() when you need to change a nested structure without affecting the original.

Worked Example: Flattening a Nested Record

import copy

record = {
    "customer": {"id": 101, "name": "Ana", "address": {"city": "Lahore"}},
    "order": {"amount": 1250.75, "items": 3},
}

def flatten(d, parent=""):
    """Turn nested dicts into {'a.b.c': value} pairs."""
    flat = {}
    for key, value in d.items():
        path = f"{parent}.{key}" if parent else key
        if isinstance(value, dict):
            flat.update(flatten(value, path))
        else:
            flat[path] = value
    return flat

for path, value in flatten(record).items():
    print(f"{path:24} {value}")

# Shallow vs deep copy
shallow = record.copy()
deep = copy.deepcopy(record)
record["customer"]["name"] = "Ana K."
print(shallow["customer"]["name"])   # Ana K.  (shared inner dict)
print(deep["customer"]["name"])      # Ana     (independent copy)

Flat a.b.c keys are convenient for logging, comparing two records or building a DataFrame; pandas.json_normalize() applies the same idea to whole lists of records.

Common Mistakes

  • Chained indexing on uncertain data: d["a"]["b"]["c"] fails on the first missing level; use a safe path helper
  • Assigning to a missing level: d["new"]["x"] = 1 raises KeyError; use setdefault() or defaultdict
  • Shallow copies of nested data: changes leak into the original; use copy.deepcopy()
  • Nesting too deeply: more than three or four levels is usually a sign a dataclass or DataFrame would be clearer

Quick Summary

  • Treat nested dicts as trees navigated by key paths
  • Use safe getters for reading and setdefault()/defaultdict for building
  • Flatten for reporting and use deepcopy() for independent copies

Conclusion

Nested dictionaries are the natural way to represent hierarchical data in Python data science. In 2026, safe creation, access, modification, and iteration over nested structures are essential skills. Combine literal syntax, dict comprehensions, safe_get, and defaultdict to build clean, robust, and maintainable pipelines that handle real-world complex data with ease.

Next steps:

  • Review any nested configuration or summary code in your projects and apply the safe access and defaultdict patterns shown above

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices