How find all childrens values of a nested dictionary, fast!

Alexander Gattin xrgtn at yandex.ru
Sat Nov 6 13:51:06 EDT 2010


Hello,

On Thu, Nov 04, 2010 at 09:20:04PM +0000, Arnaud
Delobelle wrote:
> Tough requirement, but I think I've got it.  Two
> lambdas, one reduce, one map ;)
> 
> >>> a = {'a' : {'b' :{'/' :[1,2,3,4], 'ba' :{'/' :[41,42,44]}, 'bc' :{'/':[51,52,54], 'bcd' :{'/':[68,69,66]}}},'c' :{'/' :[5,6,7,8]}},'ab' : {'/' :[12,13,14,15]}, 'ac' :{'/' :[21,22,23]}}
> >>> f = lambda v,k=None: v if k=="/" else reduce(list.__add__, map(lambda k: f(v[k],k), v), []) 

Is it that new Python3 thing? :)

P.S.

Here is my variant that still works
in python2:

def i(x):
    r = []
    for k, v in x.iteritems():
        try: r.extend(i(v))
        except: 
            if k == '/': r.extend(v)
    return r

-- 
With best regards,
xrgtn



More information about the Python-list mailing list