tree_flatten(tree, prefix='', is_leaf=None)#

Flattens a python tree to a list of key, value tuples.

The keys are using the dot notation to define trees of arbitrary depth and complexity.

from mlx.utils import tree_flatten

# [("0.0.0", 0)]

print(tree_flatten([[[0]]], ".hello"))
# [("hello.0.0.0", 0)]


Dictionaries should have keys that are valid python identifiers.

  • tree (Any) – The python tree to be flattened.

  • prefix (str) – A prefix to use for the keys. The first character is always discarded.

  • is_leaf (Callable) – An optional callable that returns True if the passed object is considered a leaf or False otherwise.


The flat representation of the python tree.

Return type:

List[Tuple[str, Any]]