Signup/Sign In
PUBLISHED ON: FEBRUARY 18, 2022

Python Program to Display Keys associated with Values in Dictionary

In this tutorial, you will learn how to write a Python program that will display the keys associated with a particular value in a dictionary. A dictionary can have multiple values associated with a single key with the help of a list. From the given dictionary we will find the key associated with a given value and print the key as our output.

Look at the examples to understand the input and output format.

Input:

dic={'x': [1, 2, 3], 'y': [11], 'z': [8, 4]}

value=[2, 4]

Output: ['x', 'z']

Input:

dic={'a': [1, 2, 3], 'b': [5, 6], 'c': [8, 4]}

value=[5, 1]

Output: ['b', 'a']

To solve this problem in python, we can use the following approaches-

  1. using a loop and items() method
  2. using list comprehension and any() method

We will discuss both approaches in detail below.

Approach 1: loop and items()

In this approach, we will use the items() method to get all the key-value pairs from the dictionary. Then we will use a for loop to traverse the list and get the key of the particular value.

The items() method returns an object that stores the list of key-value pairs of a dictionary.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a dictionary with keys and their values

Step 2- Initialise a value list for which we have to find the keys

Step 3- Run a loop to iterate through the key-values returned by items()

Step 4- Find the key of the values in the list and store them in a new list

Step 5- Print the new list as output

Step 6- The new list will have all the keys

Python Program 1

In this program, we have declared a dictionary that will have keys and a list of values. We have to find the keys of certain values which will be given in the form of a list.

# find key of a given value
# Using loop and items()
  
# initializing dictionary
dic = {'A' : [1, 3], 'B' : [4, 7], 'C' : [10, 12]}
  
# print original dictionary
print("The original dictionary : " ,dic)
  
# initializing value list 
val_list = [1,3,12]
  
# finding key
temp = {}
for key, vals in dic.items():
    for val in vals:
        temp[val] = key
key_result = [temp[ele] for ele in val_list]
  
# printing result 
print("The keys mapped to ", val_list, " are : " ,key_result) 


The original dictionary : {'A': [1, 3], 'B': [4, 7], 'C': [10, 12]}
The keys mapped to [1, 3, 12] are : ['A', 'A', 'C']

Approach 2: list comprehension and any()

In this approach, we will use the list comprehension and the any() method to find keys of some values. The any() method is a built-in method of Python that returns True if any of the items in an iterable are True, otherwise, it returns False. The iterable can be a set, list, tuple or dictionary.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a dictionary with keys and their values

Step 2- Print the original dictionary

Step 3- Initialise a value list for which we have to find the keys

Step 4- Make a new list that will store the keys to the value

Step 5- Print the new list as output

Python Program 2

In this program, we have declared a dictionary that will have keys and a list of values. To get the items of the dictionary, which are key-value pairs, we have used the items() method.

# find key of a given value
# Using list comprehension and any()
  
# initializing dictionary
d1 = {'A' : [1, 2, 3], 'B' : [5, 6], 'C' : [7, 8, 9]}
  
# print dictionary
print("The original dictionary : ",d1)
  
# initializing value list 
val_list = [6, 9]
  
# finding key
# Using list comprehension and any()
result = [key for key, val in d1.items() if any(y in val for y in val_list)]
  
# printing result 
print("The keys mapped to " ,val_list, " are : ",result)


The original dictionary : {'A': [1, 2, 3], 'B': [5, 6], 'C': [7, 8, 9]}
The keys mapped to [6, 9] are : ['B', 'C']

Conclusion

In this tutorial, you have learnt how to find keys associated with some given values in a dictionary using two different approaches. In the first approach, we have used a loop and items() method. In the second approach, we have used list comprehension and the any() method.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.