How to do a Reverse Dictionary Lookup in Python

A reverse dictionary lookup will return a list containing all the keys in a dictionary. Dictionary keys map values and are used to access values in dictionaries.

 

dictionary = {'a': 1, 'b': 2, 'c': 3}

lookup_val = 1

all_keys = []
for key, value in dictionary.items():
   if(value == lookup_val):
        all_keys.append(key)

print(all_keys)
['a']

 

Here is the same functionality but written with list comprehension so it fits on one line.

 

all_keys = [key for key, value in dictionary.items() if value == lookup_val]