Recursive tree list from dictionary

Max Erickson maxerickson at gmail.com
Sat Jan 14 20:03:25 CET 2006


David Pratt <fairwinds at eastlink.ca> wrote in
news:43C93A6B.3020503 at eastlink.ca: 

> 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: 
> 
   
>>> imd=dict()
>>> for d in source_list:
	par=d['parent']
	v=d['title']
	try:
		imd[par].append(v)
	except:
		imd[par]=[v]

		
>>> imd
{'Project': ['Geometries', 'Verticals'], 'Geometry': ['Points',
'Layers', 'Water'], 'root': ['Project', 'Geometry', 'Soil'], 'Soil':
['Soiltypes']} 
>>> 

And then do whatever you need to do from here...

max




More information about the Python-list mailing list