IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python function

unflatten

unflatten()

max.tree.unflatten(treedef, leaves, *, exact=True)

source

Rebuilds a tree from a structure and its leaves.

The inverse of flatten(), always building a fresh tree.

Parameters:

  • treedef (TreeDef) – The structure, as flatten() reported it.
  • leaves (Iterable[Any]) – One value per leaf slot, left to right; with exact, any trailing values are left unconsumed.
  • exact (bool) – When False, stop after the structure’s leaves and leave trailing values (e.g. a shared iterator’s later items) in place instead of raising. Too few leaves is always an error.

Returns:

The rebuilt tree.

Raises:

  • TypeError – If a cycle closes through a node whose class declares no __tree_empty__.
  • ValueError – If there are too few leaves for the structure (or too many when exact is True), or if treedef did not come from flatten().

Return type:

Any