Open In App

Python – Keys associated with value list in dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python dictionaries, we can have a problem finding the key of a particular value in the value list. This problem is quite common and can have applications in many domains. Let us discuss certain ways in which we can Get Keys associated with Values in the Dictionary in Python.

Example

Input: {'gfg': [4, 5], 'best': [10, 12], 'is': [8]} , list = [4,10]
Output: ['gfg', 'best']
Explanation: In this example, the keys associated with the value inside the list i.e. 4 and 10 is 'gfg' and 'best' respectively.

Get Keys Associated with Value List in Python

Below are the methods that we will cover in this article:

Keys associated with Value list using loop + items()

The combination of the above functions can be used to solve this problem. In this example, we extract all elements of the dictionary using Python items(), and a loop is used to compile the rest of the logic.

Python3




# initializing dictionary
test_dict = {'gfg' : [4, 5], 'is' : [8], 'best' : [10, 12]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing value list
val_list = [5, 10]
 
# Value's Key association
# Using loop + items()
temp = {}
for key, vals in test_dict.items():
    for val in vals:
        temp[val] = key
res = [temp[ele] for ele in val_list]
 
# printing result
print("The keys mapped to " + str(val_list) + " are : " + str(res))


Output

The original dictionary : {'gfg': [4, 5], 'is': [8], 'best': [10, 12]}
The keys mapped to [5, 10] are : ['gfg', 'best']

Keys associated with Value list using list comprehension + any()

This is yet another way in which this task can be performed. It offers shorthands that can be used to solve this problem. In this example, we use Python any() to compute if the key contains any of the values in the value list. 

Python3




# initializing dictionary
test_dict = {'gfg' : [4, 5], 'is' : [8], 'best' : [10, 12]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing value list
val_list = [5, 10]
 
# Value's Key association
# Using list comprehension + any()
res = [key for key, val in test_dict.items() if any(y in val for y in val_list)]
 
# printing result
print("The keys mapped to " + str(val_list) + " are : " + str(res))


Output

The original dictionary : {'gfg': [4, 5], 'is': [8], 'best': [10, 12]}
The keys mapped to [5, 10] are : ['gfg', 'best']

Keys associated with Value list using filter() function

In this example, we are using filter() function to find the key associated with value list. Firstly, print the original dictionary after initializing the dictionary. Create the list of dictionary values to be searched. Create a function that returns True if any element from the input list is in the dictionary values. To filter out dictionary objects that meet the above requirement, use the filter() function. Using the dict.keys() method, extract the keys of the filtered dictionary entries and store them in a Python list. Print the final outcome.

Python3




# initializing dictionary
test_dict = {'gfg' : [4, 5], 'is' : [8], 'best' : [10, 12]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing value list
val_list = [5, 10]
 
# Value's Key association
# Using filter()
def is_value_present(val_list):
    def helper(lst):
        return any(x in lst for x in val_list)
    return helper
 
filtered_dict = dict(filter
                     (lambda x: is_value_present(val_list)(x[1]),
                            test_dict.items()))
 
# extracting keys of filtered dictionary items
res = list(filtered_dict.keys())
 
# printing result
print("The keys mapped to " + str(val_list) + " are : " + str(res))


Output

The original dictionary : {'gfg': [4, 5], 'is': [8], 'best': [10, 12]}
The keys mapped to [5, 10] are : ['gfg', 'best']


Last Updated : 22 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads