archimedes.tree.unflattenยถ

archimedes.tree.unflatten(treedef: TreeDef, xs: list[ArrayLike]) Treeยถ

Reconstruct a tree from a list of leaves and a treedef.

This function is the inverse of tree_flatten. It takes a list of leaf values and a tree definition, and reconstructs the original tree structure.

Parameters:
  • treedef (TreeDef) โ€“ A tree definition, typically produced by tree_flatten or tree_structure.

  • xs (list[ArrayLike]) โ€“ A list of leaf values to be placed in the reconstructed tree. The length must match the number of leaves in treedef.

Returns:

tree โ€“ The reconstructed tree with the same structure as defined by treedef and with leaf values from xs.

Return type:

Tree

Raises:

ValueError โ€“ If the number of leaves in xs doesnโ€™t match the expected number in treedef.

See also

tree_flatten

Flatten a tree into a list of leaves and a treedef

tree_structure

Extract just the structure from a tree