On 11/11/2011 02:31 PM, macm wrote: > > Hi Folks > > > > I pass a nested dictionary to a function. > > > > def Dicty( dict[k1][k2] ): > > print k1 > > print k2 > > > > There is a fast way (trick) to get k1 and k2 as string. > > > > Whithout loop all dict. Just it! > > > > Regards > > > > macm
If my guessing was correct is this what you are looking for? nesteddict = { 'a': { 'A' : 'value1 a_A' , 'B' : 'value2 a_B' }, 'b': { 'A' : 'value3 b_A' , 'B' : 'value4 b_B' }, 'c': { 'A' : 'value3 b_A' , 'B' : 'value4 b_B' }, } def find_in_nested_dict(adict, avalue): results = [] for key1, sub_dict in adict.items(): for key2, value in sub_dict.items(): if avalue == value: results.append( (key1, key2) ) return results def mk_lookup(adict): lookup = {} for key1, sub_dict in adict.items(): for key2, value in sub_dict.items(): entry = lookup.get(value, []) entry.append( (key1, key2) ) lookup[value] = entry return lookup # good if you just want so search one value value = nesteddict['c']['B'] keys = find_in_nested_dict(nesteddict, value) print "found %r in %r" % (value, keys) # if you need many lookups perhaps better to 'precalculate a # 'reversed' dict lookup = mk_lookup(nesteddict) keys = lookup[value] print "found %r in %r" % (value, keys) -- http://mail.python.org/mailman/listinfo/python-list