Looking up a dictionary _key_ by key?

Marko Rauhamaa marko at pacujo.net
Wed Jun 24 02:55:51 EDT 2015


Ian Kelly <ian.g.kelly at gmail.com>:

> I don't think that it's fundamentally broken. A simple example would
> be the int 3, vs. the float 3, vs. the Decimal 3. All of them compare
> equal to one another, but they are distinct values, and sometimes it
> might be useful to be able to determine which one is actually a key in
> the dict.

One possibility is to enter the key on the value side as well:

    d[key] = (key, value)

    ...

    canonical_key, value = d[key]


Marko



More information about the Python-list mailing list