Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5413

Re: dict: retrieve the original key by key

From Christoph Groth <cwg@falma.de>
Subject Re: dict: retrieve the original key by key
Date 2011-05-15 11:11 +0200
References <874o4wwenu.fsf@falma.de> <BANLkTikszH_r-PAuL0pJ8reMc+bGafuRew@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1584.1305450715.9059.python-list@python.org> (permalink)

Show all headers | View raw


Chris Rebert <clp2@rebertia.com> writes:

> On Sun, May 15, 2011 at 1:28 AM, Christoph Groth <cwg@falma.de> wrote:
>> I use a huge python dictionary where the values are lists of that
>> dictionary's keys (yes, a graph).  Each key is thus referenced
>> several times.
>>
>> As the keys are rather large objects, I would like to save memory by
>> re-using key objects wherever possible, instead of having several
>> equal objects in memory.
>>
>> There does not seem to be a way to retrieve the original key from a
>> python dictionary.  Is there a technical reason for this?  (Other
>> than that such functionality was not considered to be useful enough.)
>
> Define "original key".

def original_key(dictionary, key):
    for k in dictionary:
        if k == key:
            return k
    raise KeyError(key)

But this is not efficient.

I would like to avoid having _multiple_ objects which are equal (a == b)
but not the same (a is not b).  This would save a lot of memory.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 11:11 +0200
  Re: dict: retrieve the original key by key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-15 10:18 +0000
    Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 12:46 +0200
    Re: dict: retrieve the original key by key Terry Reedy <tjreedy@udel.edu> - 2011-05-15 16:53 -0400
    Re: dict: retrieve the original key by key Chris Rebert <clp2@rebertia.com> - 2011-05-15 14:19 -0700
    Re: dict: retrieve the original key by key Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-15 18:47 -0600

csiph-web