def build_tree_recursive(tree, parent, nodes):
    children = [n for n in nodes if n.parent == parent]
for child in children:
    tree[child.name] = {}
build_tree_recursive(tree[child.name], child, nodes)
Преобразование древовидного списка в иерархию dict