Recursive tree list from dictionary

David Pratt fairwinds at
Sat Jan 14 12:52:43 EST 2006

Hi. I am wanting to create a tree list result structure from a 
dictionary to categorize results. The dictionary contains elements that 
identify its parent. The levels of categorization is not fixed, so there 
is a need for the code to be recursive to drill down to the lowest 
level. I have contrived a small source and result list to illustrate:

source_list =[
	{'title': 'Project', 'parent':'root'},
	{'title': 'Geometry', 'parent':'root'},
	{'title': 'Soil', 'parent':'root'},
	{'title': 'Geometries', 'parent':'Project'},
	{'title': 'Verticals', 'parent':'Project'},
	{'title': 'Points', 'parent':'Geometry'},
	{'title': 'Layers', 'parent':'Geometry'},
	{'title': 'Water', 'parent':'Geometry'},
	{'title': 'Soiltypes', 'parent':'Soil'}]
What I want to do is a tree list like this:

tree_result_list = [
	# Project
	['Project', ['Geometries','Verticals']],
	# Geometry
	['Geometry', ['Points', 'Layers', 'Water']],
	# Soil
	['Soil', ['Soiltypes']]

I have not been successful and am hoping someone can 	
advise a solution to accomplish this. Many thanks.


More information about the Python-list mailing list