🪆 Nested Dict Tools
Nested Dict Tools is a Python package that provides utilities for working with nested dictionaries. It includes:
- Recursive types for describing nested mappings and dictionaries.
- Fully typed functions to:
- Flatten and unflatten nested dictionaries.
- Get and set deeply nested values.
- Iterate over leaves and the dictionary that contains then.
- Filter and map functions on leaves.
- Other helpful functions.
from nested_dict_tools import (
filter_leaves,
flatten_dict,
get_deep,
is_in_leaves,
iter_leaf_containers,
iter_leaves,
map_leaves,
set_deep,
unflatten_dict,
)
nested = {"a": {"b": {"c": 42}}}
# Get a deeply nested value
value = get_deep(nested, ["a", "b"])
print(value) # Output: {'c': 42}
# Set a deeply nested value
set_deep(nested, ["a", "z"], "new_value")
print(nested) # Output: {'a': {'b': {'c': 42}, 'z': 'new_value'}}
# Flatten the nested dictionary
flat = flatten_dict(nested, sep=".")
print(flat) # Output: {'a.b.c': 42, 'a.z': 'new_value'}
# Unflatten the flattened dictionary
unflattened = unflatten_dict(flat, sep=".")
print(unflattened == nested) # Output: True
# Iterate over leaves
leaves = list(iter_leaves(nested))
print(leaves) # Output: [42, 'new_value']
# Iterate over leaf containers and keys
leaf_refs = list(iter_leaf_containers(nested))
print(leaf_refs) # Output: [({'c': 42}, 'c'), ({'b': {'c': 42}, 'z': 'new_value'}, 'z')]
# and mutate leaves in place
d, key = leaf_refs[0]
d[key] = 3
print(nested) # Output: {'a': {'b': {'c': 3}, 'z': 'new_value'}}
# Check if a values is the leaves
print(is_in_leaves("foo", nested)) # Output: False
# Filter leaves
nested = filter_leaves(
nested,
lambda k, v: isinstance(v, int),
)
print(nested) # Output: {'a': {'b': {'c': 3}}}
# Map on leaves
mapped = map_leaves(lambda x: x + 1, nested)
print(mapped) # Output: {'a': {'b': {'c': 4}}}
# Map on leaves with several dictionaries
mapped = map_leaves(lambda x, y: x + y + 1, nested, nested)
print(mapped) # Output: {'a': {'b': {'c': 7}}}
# # Recursive types:
type NestedDict[K, V] = dict[K, NestedDictNode[K, V]]
type NestedDictNode[K, V] = V | NestedDict[K, V]
# Similar types for Mapping and MutableMapping
⬇️ Installation
You can install Nested Dict Tools via pip:
pip install nested-dict-tools
🧾 License
Metadata
Release files for nested_dict_tools 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nested_dict_tools-0.4.1.tar.gz | 77.0 kB | Without provenance | Details for nested_dict_tools-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nested_dict_tools-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for nested_dict_tools-0.4.1-py3-none-any.whl |
Total release size: 85.7 kB
Release files / nested_dict_tools-0.4.1.tar.gz
| Download URL | nested_dict_tools-0.4.1.tar.gz |
|---|---|
| Size | 77.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f778e9eca2723ee0e7dd5deda7abc855798405e47cf57dba01c501c48004ef6
|
|
BLAKE2b-256 checksum How to use checksums |
cd083fcb1da1364d30e068799cfbd982e3590e443cc017c87e17d06c33ba4865
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|
Release files / nested_dict_tools-0.4.1-py3-none-any.whl
| Download URL | nested_dict_tools-0.4.1-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a3ac64a8e755b878c5e6cf4f527a9cb3a7564568dee166947879e3406ad034bb
|
|
BLAKE2b-256 checksum How to use checksums |
95e0a1382ae821e0482270175495a10e5e0112e133f5c825704d9bdac6fad1e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|
Security for nested_dict_tools 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.