Find Key by Value in Python Dictionary

September 11, 2010

It's easy to find the value given a key to a python dictionary:

lValue = lDictionary[lKey]

If you want to do the reverse, and find the value for a given key, it's possible by using a list comprehension:

lKey = [key for key, value in lDictionary.iteritems() if value == lValue][0]

Tags: python dictionary