Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; 'indexing': 0.07; '[1,': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; '23,': 0.16; 'cc:name:python list': 0.16; 'dictionary,': 0.16; 'key?': 0.16; 'received:mail-qk0-x22a.google.com': 0.16; 'storing': 0.16; 'subject:key': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '"",': 0.22; 'keys': 0.22; '2015': 0.23; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'second': 0.24; 'message-id:@mail.gmail.com': 0.28; 'dan': 0.29; 'dictionary': 0.29; 'knows': 0.32; 'getting': 0.33; 'traceback': 0.33; 'thanks!': 0.34; 'subject:?': 0.34; 'file': 0.34; 'received:google.com': 0.34; 'there': 0.36; 'subject:: ': 0.37; 'tue,': 0.38; 'pm,': 0.39; 'method': 0.39; 'does': 0.39; 'provide': 0.61; 'request.': 0.66; '8bit%:40': 0.66; 'skip:\xe2 10': 0.70; 'skip:/ 30': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=V3lOxD46ARrCGZZnLJTNyJgJyiE1xoGVMS2e74NEdOA=; b=RVofdG677ZEGrTyMgJAVOr3Ols2XHYgM/3ycDvQ/mqjBD3ag+89fi5ttwuQR7jDFvM 6cFmhNMhktf2MG0NNekzeLEhdRqvrFTeq/lTGHm6/ZhZYkiOKxVkafK7KL3/yPPVZ/BC Q4C1Za4wqcjsM79ydVQm1i/RNOvqfWL+fan5C+aZ+mEFJnWiq1iEw1Z1kVnpY5JJfwUp lE1pPVq86w7xvYiHNmWXKF5eYT0EuUrGqzhiUa6s+2hX8k7Phf4+fJUSbWXtjn3Xate7 50hKpHQpKwtn+Xg+I15IVBwBbWdYXCPtMxkr/RzyhedBvXKgx7DPPOzYdFvnPP+4WNx7 gaCA== MIME-Version: 1.0 X-Received: by 10.55.15.91 with SMTP id z88mr7453133qkg.87.1435106878721; Tue, 23 Jun 2015 17:47:58 -0700 (PDT) In-Reply-To: <851th2t05t.fsf@benfinney.id.au> References: <851th2t05t.fsf@benfinney.id.au> Date: Tue, 23 Jun 2015 17:47:58 -0700 Subject: Re: Looking up a dictionary _key_ by key? From: Dan Stromberg To: Ben Finney Cc: Python List Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435107329 news.xs4all.nl 2840 [2001:888:2000:d::a6]:57927 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93054 On Tue, Jun 23, 2015 at 5:33 PM, Ben Finney wr= ote: > 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 > =E2=80=98dict.keys=E2=80=99 method to get them as a collection. So it appears: $ ./pythons --command 'd=3D{1:2, 2:4, 3:8}; print(d.keys())' /usr/local/cpython-2.4/bin/python good [1, 2, 3] /usr/local/cpython-2.5/bin/python good [1, 2, 3] /usr/local/cpython-2.6/bin/python good [1, 2, 3] /usr/local/cpython-2.7/bin/python good [1, 2, 3] /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/jython-2.7rc1/bin/jython good [3, 2, 1] /usr/local/pypy-2.5.0/bin/pypy good [1, 2, 3] /usr/local/pypy3-2.4.0/bin/pypy3 good dict_keys([1, 2, 3]) But: $ ./pythons --command 'd=3D{1:2, 2:4, 3:8}; print(d.keys()[1])' /usr/local/cpython-2.4/bin/python good 2 /usr/local/cpython-2.5/bin/python good 2 /usr/local/cpython-2.6/bin/python good 2 /usr/local/cpython-2.7/bin/python good 2 /usr/local/cpython-3.0/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/cpython-3.1/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/cpython-3.2/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/cpython-3.3/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/cpython-3.4/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/cpython-3.5/bin/python bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object does not support indexing /usr/local/jython-2.7rc1/bin/jython good 2 /usr/local/pypy-2.5.0/bin/pypy good 2 /usr/local/pypy3-2.4.0/bin/pypy3 bad Traceback (most recent call last): File "", line 1, in TypeError: 'dict_keys' object is not subscriptable Would I have to do an O(n) search to find my key? Thanks!