{"library":"optree","code":"from optree import tree_map\n\ndef add_one(x):\n    return x + 1\n\ntree = {'a': 1, 'b': [2, 3], 'c': {'d': 4}}\nmapped_tree = tree_map(add_one, tree)\nprint(mapped_tree)\n\nfrom optree import tree_flatten, tree_unflatten, PyTreeSpec\n\nleaves, treespec = tree_flatten(tree)\nprint(f\"Leaves: {leaves}\")\nprint(f\"TreeSpec: {treespec}\")\n\nreconstructed_tree = tree_unflatten(treespec, [l * 10 for l in leaves])\nprint(f\"Reconstructed tree with modified leaves: {reconstructed_tree}\")","lang":"python","description":"This quickstart demonstrates the core `tree_map` function to apply a transformation to all leaves of a PyTree, and also shows how to flatten a PyTree into leaves and a structure (`treespec`) and then unflatten it back, potentially with modified leaves.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":0}]}