I need to create a tree-like structure in Python. I have a function get(parentId), which returns a list of objects with that parent — I think that should be done recursively.
The result should be something like this: ["root object", ["child1 of root", "child2 of root", ["child2-1", "child2-2"]]]
Every object has a attribute parent, which is is the parentId for get(), but as a starting point I only have the root object.
Assuming you are still interested in a list representation of the tree (which is not necessarily a useless thing to do), this is a recursive function definition that I believe does what you need (provided that the
get()function has indeed been defined before):You could use it in a way similar to this: