You can access the key through the value:

Asked

Viewed 736 times

3

In dictionaries, in Python, is it possible to access a key through the value? Is there any method that inverts key/value?

1 answer

5


The dict has no native method for this. But there is a maneuver that can be done.

You can use the method values, that will return a list. Then you take the index of this list and get the values of the other list generated by the method keys.

Confused? So let’s take an example:

valores = {"valor_16" : 16, "valor_17" : 17}

valores.keys()[valores.values().index(16)]; // "valor_16"

Original answer in SOEN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.