Hi Folks Thanks a lot
Script from Diez works: print list(f(a)) but should be print list(f(a['a']['b'])) to fit my example. About Peter script I am receiving >>> for v in f(a['a']['b']): ... b.extend(v) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: 'int' object is not iterable I am trying understand this error. Best Regards and thanks a lot again! Mario macm On 4 nov, 15:26, Peter Otten <__pete...@web.de> wrote: > macm wrote: > > How find all childrens values of a nested dictionary, fast! > > >>>> 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]}} a['a'] > > {'c': {'/': [5, 6, 7, 8]}, 'b': {'ba': {'/': [41, 42, 44]}, '/': [1, > > 2, 3, 4], 'bc': {'bcd': {'/': [68, 69, 66]}, '/': [51, 52, 54]}}} > >>>> a['a']['b'] > > {'ba': {'/': [41, 42, 44]}, '/': [1, 2, 3, 4], 'bc': {'bcd': {'/': > > [68, 69, 66]}, '/': [51, 52, 54]}} > >>>> a['a']['b'].values() > > [{'/': [41, 42, 44]}, [1, 2, 3, 4], {'bcd': {'/': [68, 69, 66]}, '/': > > [51, 52, 54]}] > > > Now I want find all values of key "/" > > > Desire result is [41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54] > > > I am trying map, reduce, lambda. > > Hmm, I'm trying none of these and get a different result: > > >>> def f(d): > > ... stack = [d.iteritems()] > ... while stack: > ... for k, v in stack[-1]: > ... if k == "/": > ... yield v > ... else: > ... stack.append(v.iteritems()) > ... break > ... else: > ... stack.pop() > ...>>> b = [] > >>> for v in f(a): > > ... b.extend(v) > ...>>> b > > [5, 6, 7, 8, 41, 42, 44, 1, 2, 3, 4, 68, 69, 66, 51, 52, 54, 21, 22, 23, 12, > 13, 14, 15] > > What am I missing? > > Peter -- http://mail.python.org/mailman/listinfo/python-list