Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:keys': 0.09; 'value:': 0.09; 'skip:f 30': 0.13; 'def': 0.14; 'folks': 0.15; '%r"': 0.16; "'a'": 0.16; '[])': 0.16; 'guessing': 0.16; 'keys)': 0.16; 'lookup': 0.16; 'lookups': 0.16; 'wrote:': 0.18; 'dictionary': 0.23; 'header:In-Reply-To:1': 0.23; 'pass': 0.28; 'print': 0.29; 'value)': 0.30; "skip:' 10": 0.30; 'pm,': 0.31; 'correct': 0.31; 'to:addr:python-list': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'header:X-Complaints- To:1': 0.33; 'loop': 0.34; 'function.': 0.34; 'keys': 0.34; 'nested': 0.34; 'for?': 0.36; 'entry': 0.37; 'received:org': 0.37; 'perhaps': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'results': 0.61; 'subject:Get': 0.71 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: Get keys from a dicionary Date: Fri, 11 Nov 2011 18:45:00 +0100 References: <8f5215a8-d08f-4355-a5a2-77fcaa32c92d@j10g2000vbe.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: unicorn.dungeon.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110921 Lightning/1.0b2 "" In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321033516 news.xs4all.nl 6965 [2001:888:2000:d::a6]:47772 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15602 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)