convert flat structure into hierarchical one

Ksenia Marasanova ksenia at ksenia.nl
Sun Sep 26 13:49:41 EDT 2004


I get this kind of list from a database. (The tuple structure is: id, 
name, parent_id)

[(1, 'grandparent', None), (2, 'parent', 1), (3, 'otherparent', 1), (4, 
'child', 3)]

I would like to transfer it (in Python) into a tree structure. I don't 
care much about format, as long as I'll be able to get all the 
information, based on one id. For example, if I have id=3, I want to 
get
- name ('otherparent')
- children (one child, with id=4, name='child')
- parent id

Any tips? Recipes? ;-)


Thanks,
Ksenia.




More information about the Python-list mailing list