In this article we will discuss how to find all keys associated with a given single value or multiple values.

Suppose we have a dictionary of words and thier frequency i.e.

Now we want to get all the keys in dictionary whose value is 43. Like in our case there will two keys who has value 43 i.e.

Now let’s see how to get the list of keys by given value

Find keys by value in dictionary

As, dict.items() returns an iterable sequence of all key value pairs in dictionary. So, we will iterate over this sequence and for each entry we will check if value is same as given value then we will add the key in a separate list i.e.

Now let’s use this function to get keys by value 43 i.e.

Output:

Same can be achieved by List Comprehensions i.e.

Find keys in dictionary by value list

Suppose we want to find all the keys in dictionary whose value matches with any of the value given in list i.e.

To do that we will iterate over iterable sequence returned by dict.items() and for each entry we will check if its value matches with any entry from the given value list, if yes then we will add that key in a separate list i.e.

Now lets use this to find all the keys from dictionary whose values is equal to any value from the list i.e.

Output:

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.