Search code examples
pythonpython-2.7listdictionaryreverse-lookup

How to get a key from a list of dictionaries by its value


I have a list of dictionaries and I am trying to get the dictionary key by its value:

list1 = [{'a':1,'b':2,'c':3},
         {'d':4,'e':5,'f':6},
         {'g':7,'h':8,'i':9}]

Here I am trying to get the dictionary key if the value 5 exists in the list of dictionaries.


Solution

  • for dic in list1:
        for key in dic:
            if dic[key] == search_value:
                print key