Recursive tree list from dictionary

Alan Franzoni alan.franzoni.xyz at gmail.com
Sat Jan 14 14:39:07 EST 2006


Il Sat, 14 Jan 2006 13:52:43 -0400, David Pratt ha scritto:

> source_list =[

I don't understand what you mean by saying that 'levels of categorization
is not fixed', are there more than two keys in any dictionary?

Basically, thus, you have a list of dictionaries and you want to get a list
of lists, right? You haven't specified what order would you like the list
to be sorted, though.  Maybe you would benefit from use and ordered
dictionary implementation like orderedDict, but such behaviour can be
created anyway. I would use a kind of 'helper dictionary' to map the list
position to the root element:

def convertList(dictlist):
    helpdict={}
    for elem in dictlist:
        helpdict.setdefault(elem['parent'],[])
        helpdict[elem['parent']].append(elem['title'])
  
    return [k,v for k,v in helpdict.items() if k!='root']



-- 
Alan Franzoni <alan.franzoni.xyz at gmail.com>
-
Togli .xyz dalla mia email per contattarmi.
To contact me, remove .xyz from my email address.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E



More information about the Python-list mailing list