Fix Python – Inverse dictionary lookup in Python

Question

Asked By – RadiantHex

Is there any straightforward way of finding a key by knowing the value within a dictionary?

All I can think of is this:

key = [key for key, value in dict_obj.items() if value == 'value'][0]

Now we will see solution for issue: Inverse dictionary lookup in Python


Answer

There is none. Don’t forget that the value may be found on any number of keys, including 0 or more than 1.

This question is answered By – Ignacio Vazquez-Abrams

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0