Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!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.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'value,': 0.03; 'item,': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type;': 0.09; 'python': 0.11; ':-)': 0.14; '23,': 0.16; 'dictionary,': 0.16; 'key?': 0.16; 'keys.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'storing': 0.16; 'subject:key': 0.16; 'wrote:': 0.16; 'comparing': 0.18; 'compare': 0.20; 'keys': 0.22; '2015': 0.23; 'second': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'dan': 0.29; 'dictionary': 0.29; 'values': 0.30; 'waste': 0.31; 'knows': 0.32; 'getting': 0.33; 'point': 0.33; 'values.': 0.33; 'subject:?': 0.34; 'to:addr:python-list': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; '2005': 0.36; 'subject:: ': 0.37; 'tue,': 0.38; 'received:org': 0.38; 'say': 0.38; 'supports': 0.38; 'pm,': 0.39; 'method': 0.39; 'whatever': 0.39; 'to:addr:python.org': 0.39; 'some': 0.40; "you'll": 0.61; 'provide': 0.61; 'more': 0.62; 'request.': 0.66; '8bit%:40': 0.66; 'promise': 0.66; 'wanting': 0.66; 'talking': 0.67; 'skip:\xe2 10': 0.70; 'long-term': 0.72; 'special': 0.72; '_o__)': 0.84; 'received:125': 0.84; 'skip:/ 30': 0.84; 'shopping': 0.87; 'mountains': 0.91; '\xe2\x80\x9cthe': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Looking up a dictionary _key_ by key? Date: Wed, 24 Jun 2015 11:07:46 +1000 References: <851th2t05t.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:cNMqXbxkRFgVgsXo+E8YeAviaZI= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435108074 news.xs4all.nl 2859 [2001:888:2000:d::a6]:37768 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93058 Dan Stromberg writes: > On Tue, Jun 23, 2015 at 5:33 PM, Ben Finney wrote: > > Dan Stromberg writes: > > > >> Is there a way of getting the key used by the dictionary, short of > >> storing a reference to it in the value, or using a second dictionary? > > > > The dictionary knows its keys and can provide them on request. Call the > > ‘dict.keys’ method to get them as a collection. > > So it appears: > > $ ./pythons --command 'd={1:2, 2:4, 3:8}; print(d.keys())' […] > /usr/local/cpython-3.0/bin/python good > /usr/local/cpython-3.1/bin/python good dict_keys([1, 2, 3]) > /usr/local/cpython-3.2/bin/python good dict_keys([1, 2, 3]) > /usr/local/cpython-3.3/bin/python good dict_keys([1, 2, 3]) > /usr/local/cpython-3.4/bin/python good dict_keys([1, 2, 3]) > /usr/local/cpython-3.5/bin/python good dict_keys([1, 2, 3]) > /usr/local/pypy3-2.4.0/bin/pypy3 good dict_keys([1, 2, 3]) Note that none of these (from Python 3) are lists; I don't promise a list, only a collection :-) > Would I have to do an O(n) search to find my key? If you're wanting to *locate* a particular item, you'll need to find it using whatever comparisons the values support. You described some custom type; the answer will depend on what that type supports for comparing values. What I can say at this point is: we are no longer talking about anything special to dictionary keys. You just have a collection of values, which you can iterate over and compare to some target. -- \ “The long-term solution to mountains of waste is not more | `\ landfill sites but fewer shopping centres.” —Clive Hamilton, | _o__) _Affluenza_, 2005 | Ben Finney