dict-deep

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

A lightweight library providing deep_set and deep_get functions to access nested dictionaries (or any object) using dot-notation strings. Current version 4.1.2, Python >=3.5,<4.0. Maintained but infrequent releases.

pip install dict-deep
error ModuleNotFoundError: No module named 'dict_deep'
cause Attempting to import using hyphen in the package name: 'import dict-deep' is invalid.
fix
Use the correct import: from dict_deep import deep_get
error TypeError: 'int' object is not subscriptable
cause deep_set tries to traverse into an existing leaf value (e.g., an integer) to create nested dicts, but it can't override a non-dict.
fix
Ensure that intermediate keys point to a dict or are not set as leaf values before calling deep_set.
gotcha deep_set modifies the original dict in place; it does not return a new dict.
fix Be aware that the original dict is mutated. If you need immutability, deepcopy before calling.
gotcha Keys with dots in them are ambiguous: 'a.b' in dot-notation will be interpreted as a path, not a literal key.
fix Use a list of keys instead of a dotted string when keys contain dots: deep_get(d, ['a.b'])
deprecated deep_update was removed in v4.0. Use deep_set or manual update instead.
fix Replace deep_update calls with deep_set for individual keys, or use dictionary update methods.

Demonstrates basic deep_get and deep_set with dot-notation strings.

from dict_deep import deep_get, deep_set

nested = {'a': {'b': {'c': 42}}}
# Get value using dot-separated key
val = deep_get(nested, 'a.b.c')
print(val)  # 42

# Set value using dot-separated key
deep_set(nested, 'a.b.c', 99)
print(nested['a']['b']['c'])  # 99