tree data structure

Max M maxm at mxm.dk
Fri Sep 6 15:24:30 CEST 2002


Raymond Hettinger wrote:

>>>>tree = {}
>>>>for key, value in original:
>>>
>>>  tree.setdefault(key,[]).append(value)


That is a darn nice way to do it, even though it's a bit more readable to::

     for key, value in original:
         if map.has_key(key):
             map[key].append(value)
         else:
             map[key] = [value]

I just hate to see something I have done a lot of times in four line 
boiled down to one... :-)

I knew the setdefault() method, but would probably never have thought of 
using it like that on my own.


regards Max M




More information about the Python-list mailing list