Open In App
Related Articles

Python program to extract dictionary items for custom values

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a dictionary, extract all the items that match given values in the list

Example:

Input : test_dict = {“Gfg” : 3, “is” : 5, “for” : 8, “Geeks” : 10, “Best” : 16 }, sub_list = [5, 4, 10, 20, 16, 8] 
Output : {‘is’: 5, ‘Geeks’: 10, ‘Best’: 16, “for” : 8} 
Explanation : All values matching list values extracted along with keys.


Input : test_dict = {“Gfg” : 3, “is” : 5, “for” : 8, “Geeks” : 10, “Best” : 16 }, sub_list = [5, 4, 10] 
Output : {‘is’: 5, ‘Geeks’: 10} 
Explanation : All values matching list values extracted along with keys. 
 

Method 1: Using a loop

Use of a ‘for loop’ is one of the ways in which this task can be performed. In this, we iterate for all the keys and check if the value is present in a custom list, if yes, then it’s returned.

Python3

# initializing dictionary
test_dict = {"Gfg": 3, "is": 5, "for": 8,
             "Geeks": 10, "Best": 16}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [5, 4, 10, 20, 16]
 
# Using loop to perform iteration
res = dict()
 
for key in test_dict:
   
    if test_dict[key] in sub_list:
        res[key] = test_dict[key]
 
# printing result
print("Extracted items : " + str(res))

                    

Output:

The original dictionary is : {‘Gfg’: 3, ‘is’: 5, ‘for’: 8, ‘Geeks’: 10, ‘Best’: 16} 
Extracted items : {‘is’: 5, ‘Geeks’: 10, ‘Best’: 16}

Method 2: Using dictionary comprehension

This is a one-liner with the help of which this task can be performed. In this, we iterate for all keys using dictionary comprehension in one-liner approach.

Python3

# initializing dictionary
test_dict = {"Gfg": 3, "is": 5, "for": 8,
             "Geeks": 10, "Best": 16}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [5, 4, 10, 20, 16]
 
# dictionary comprehension to compile logic in one dictionary
# in operator used to check value existence
res = {key: val for key, val in test_dict.items() if val in sub_list}
 
# printing result
print("Extracted items : " + str(res))

                    

Output:

The original dictionary is : {‘Gfg’: 3, ‘is’: 5, ‘for’: 8, ‘Geeks’: 10, ‘Best’: 16} 
Extracted items : {‘is’: 5, ‘Geeks’: 10, ‘Best’: 16}

Method 3: Using set intersection

Approach

we can convert both the dictionary values and the given list into sets, and then perform a set intersection operation to get the common values. Finally, we can create a new dictionary containing the items with the common values.

Algorithm

1. Convert the dictionary values into a set using the set() function.
2. Convert the given list into a set using the set() function.
3. Perform a set intersection operation on the two sets using the & operator.
4. Create a new dictionary containing the items with the common values.
5. Return the new dictionary.

Python3

def extract_items(test_dict, sub_list):
    dict_values_set = set(test_dict.values())
    sub_list_set = set(sub_list)
    common_values = dict_values_set & sub_list_set
    filtered_dict = {k: v for k, v in test_dict.items() if v in common_values}
    return filtered_dict
test_dict = {"Gfg": 3, "is": 5, "for": 8,
             "Geeks": 10, "Best": 16}
sub_list = [5, 4, 10, 20, 16]
print(extract_items(test_dict, sub_list))

                    

Output
{'is': 5, 'Geeks': 10, 'Best': 16}

Time Complexity: O(N), where N is the number of items in the dictionary.
Space Complexity: O(N), where N is the number of items in the dictionary.

Method 4 : using the filter() function along with a lambda function. 

The steps involved in this approach are as follows:

Initialize the dictionary test_dict and list sub_list.
Use the filter() function along with a lambda function to filter the items from the dictionary test_dict based on whether the value is present in the sub_list or not.
Convert the filtered items into a dictionary using the dict() constructor.
Print the extracted items.

Python3

# initializing dictionary
test_dict = {"Gfg": 3, "is": 5, "for": 8,
             "Geeks": 10, "Best": 16}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing list
sub_list = [5, 4, 10, 20, 16]
 
# Using filter() function to extract items
res = dict(filter(lambda item: item[1] in sub_list, test_dict.items()))
 
# printing result
print("Extracted items : " + str(res))

                    

Output
The original dictionary is : {'Gfg': 3, 'is': 5, 'for': 8, 'Geeks': 10, 'Best': 16}
Extracted items : {'is': 5, 'Geeks': 10, 'Best': 16}

The time complexity of this approach is O(n), where n is the number of items in the dictionary test_dict.

 The auxiliary space complexity is also O(n), as a new dictionary is created to store the filtered items.



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