Looking up a dictionary _key_ by key?

MRAB python at mrabarnett.plus.com
Tue Jun 23 21:32:52 EDT 2015


On 2015-06-24 01:21, Dan Stromberg wrote:
> I know that sounds strange: usually we look up values by key, not keys.
>
> But suppose you have a strange key type that despite being "equal", is
> not identical in some fields, and you need to see those fields.
>
> 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?
>
Yes!

Start with a dict:

 >>> d = {1.0: 'one', 2: 'two', 3+0j: 'three'}
 >>> d[1]
'one'
 >>> d[2]
'two'
 >>> d[3]
'three'

Suppose we want the key that matches 3:

 >>> k = 3

Get the keys as a set:

 >>> s = set(s)
 >>> s
{1.0, 2, (3+0j)}

Using intersection doesn't return what we want:

 >>> s & {k}
{3}
 >>> {k} & s
{3}

so we have to get creative.

Remove the keys that _don't_ match:

 >>> s - {k}
{1.0, 2}

and then remove them from the set:

 >>> s - (s - {k})
{(3+0j)}

Finally, get the key:

 >>> (s - (s - {k})).pop()
(3+0j)

Simple, really! :-)




More information about the Python-list mailing list