dm-tree
Tree is a library for working with nested data structures.
What it is and what it does
dm-tree is a library for manipulating nested data structures—dictionaries, lists, tuples, and their combinations—by applying operations uniformly to leaf values while preserving the overall container structure. It generalizes Python's built-in `map` function from flat sequences to arbitrarily nested hierarchies. The core operations are `flatten` (convert a nested structure to a flat list), `map_structure` (apply a function to each leaf), and traversal utilities. The implementation is backed by optimized C++ code designed for performance-critical applications like machine learning pipelines that process large nested tensors or parameter trees.
The package depends on absl-py, attrs, numpy, and wrapt, and targets modern Python (3.10+). It is actively maintained, with recent releases and no known security vulnerabilities. The library is particularly useful in machine learning frameworks where nested structures of tensors or parameters are common, though it applies to any domain working with complex hierarchical data.
Use it for:
- Flatten nested parameter dictionaries in machine learning models for batch processing or serialization.
- Apply transformations uniformly across all leaves of a nested tensor structure while preserving hierarchy.
- Traverse and validate deeply nested configuration or data structures in data pipelines.
- Convert between nested and flat representations when interfacing with APIs that expect one or the other.
- Implement recursive operations on tree-like data without manual recursion boilerplate.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides utilities for working with nested data structures—flattening, mapping functions across leaves, and traversing trees while preserving structure—backed by an optimized C++ implementation.
Yes. dm-tree is actively maintained, has no security vulnerabilities, carries a permissive Apache 2.0 license, and solves a genuine problem in working with nested structures. The medium install friction (compiled wheels) is well-mitigated by broad platform coverage and modern Python support. Install it if your code regularly manipulates nested dicts, lists, or mixed hierarchies—especially in machine learning or data processing contexts.
Install
dm-tree on PyPI
pip
pip install dm-treeuv
uv add dm-treepoetry
poetry add dm-treeInstalling dm-tree
Before you install
Medium install friction due to compiled C++ components requiring platform-specific wheels; however, wheels are available for modern Python versions (3.10–3.14) across macOS, Linux (x86_64 and aarch64), and Windows, and the package is actively maintained with a recent release.
License in practice
Licensed under Apache 2.0 (permissive), allowing free use, modification, and distribution in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install dm-tree
import tree
structure = [[1], [[[2, 3]]], [4]]
flat = tree.flatten(structure) # [1, 2, 3, 4]
mapped = tree.map_structure(lambda v: v**2, structure) # [[1], [[[4, 9]]], [16]]
Requires Python 3.10 or later; installation pulls in absl-py, attrs, numpy, and wrapt as runtime dependencies.
Verify before relying
- Whether the C++ backend provides measurable performance gains for typical use cases versus pure Python alternatives.
- API stability guarantees or backward-compatibility policy given the Beta development status.
Package facts
| License | Apache 2.0 (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | 4 — absl-py, attrs, numpy, wrapt |
| Maintenance | actively maintained — 136 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 5,699,060/month — #2,050 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: dm_tree-0.1.10-cp310-cp310-macosx_10_9_universal2.whl; dm_tree-0.1.10-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; dm_tree-0.1.10-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; dm_tree-0.1.10-cp310-cp310-win_amd64.whl; dm_tree-0.1.10-cp311-cp311-macosx_10_9_universal2.whl; dm_tree-0.1.10-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; dm_tree-0.1.10-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; dm_tree-0.1.10-cp311-cp311-win_amd64.whl; dm_tree-0.1.10-cp312-cp312-macosx_10_13_universal2.whl; dm_tree-0.1.10-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; dm_tree-0.1.10-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; dm_tree-0.1.10-cp312-cp312-win_amd64.whl; dm_tree-0.1.10-cp313-cp313-macosx_10_13_universal2.whl; dm_tree-0.1.10-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; dm_tree-0.1.10-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; dm_tree-0.1.10-cp313-cp313t-macosx_10_13_universal2.whl; dm_tree-0.1.10-cp313-cp313t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl; dm_tree-0.1.10-cp313-cp313t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl; dm_tree-0.1.10-cp313-cp313-win_amd64.whl; dm_tree-0.1.10-cp314-cp314-macosx_10_15_universal2.whl
Keywords: tree, nest, flatten
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
optreeOpTree provides optimized utilities for…
permissive · top 5,000 on PyPI
pathablePathable provides path-like objects for…
permissive · top 1,000 on PyPI
json-normalizeRecursively flattens nested JSON-like…
permissive · top 15,000 on PyPI
banalProvides type-checking and data-cleaning…
permissive · top 15,000 on PyPI
flatdictFlattens nested dictionaries into single-level…
permissive · top 5,000 on PyPI
llistProvides doubly and singly linked list data…
permissive · top 15,000 on PyPI
json-flattenConverts nested JSON objects into flat…
permissive · top 15,000 on PyPI
treelibProvides a simple tree data structure…
permissive · top 5,000 on PyPI
pluckyPlucky provides concise, safe access to deeply…
permissive · top 15,000 on PyPI
glomglom provides path-based access and declarative…
permissive · top 1,000 on PyPI