ndicts

raw JSON →
0.3.0 verified Fri May 01 auth: no python

ndicts provides a nested dictionary class (ndict) that supports dot-notation access and manipulation of nested structures. Version 0.3.0 is the latest, targeting Python 3.8+. Release cadence is sporadic.

pip install ndicts
error KeyError: 'a.b'
cause Using dot notation on a NestedDict that doesn't have nested keys, or the key is ambiguous because a key literally contains a dot.
fix
Use tuple keys like ('a', 'b') or check the key exists with 'in'.
error AttributeError: 'NestedDict' object has no attribute 'to_dict'
cause Calling to_dict() on older version before 0.3.0; method may be named differently or not exist.
fix
Upgrade to latest: pip install --upgrade ndicts. On older versions use dict(nd) or nd.flat().
gotcha Dot notation keys like 'a.b' will be split on dots. If your actual keys contain dots, use a different delimiter or avoid dot-notation access.
fix Use bracket access with tuple keys or set a custom delimiter via NestedDict(delimiter='|').
gotcha NestedDict does not preserve insertion order in Python <3.7 (though Python 3.8+ does). Relying on order may break in older Python versions if backporting.
fix Use Python 3.7+ or wrap in OrderedDict if necessary.

Create a NestedDict, access and set values with dot-notation keys, and convert back to a plain dict.

from ndicts import NestedDict
nd = NestedDict({'a': {'b': 1}})
print(nd['a.b'])  # access via dot notation
nd['a.c'] = 2
print(nd.to_dict())