Sometimes, while working with Python dictionaries, we can have a problem in which we need to retain only those keys, whose values are part of the target list. This kind of problem can have potential problems in many domains including web development. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = {'gfg': 3} tar_list = [3, 4, 10] (Values to retain) Output : {'gfg': 3}
Input : test_dict = {'gfg': 5, 'best': 12} tar_list = [3, 4, 10] (Values to retain) Output : {}
Method #1: Using dictionary comprehension
This is one of the ways in which this problem can be solved. In this, we perform the task of filtering using conditional expression inside comprehension, retaining only those items present list.
Python3
# Python3 code to demonstrate working of # Retain list elements value items # Using dictionary comprehension # Initializing dictionary test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } # Printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing target list tar_list = [ 3 , 4 , 10 ] # Retain list elements value items # Using dictionary comprehension res = {key: val for key, val in test_dict.items() if val in tar_list} # Printing result print ( "The filtered dictionary : " + str (res)) |
The original dictionary is : {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'Lazyroar': 10} The filtered dictionary : {'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time complexity: O(N)
Auxiliary Space: O(N)
Method #2: Using filter() + lambda
The combination of the above functions can be used to solve this problem. In this, we perform the task of filtration using filter, logic fed using lambda function.
Python3
# Python3 code to demonstrate working of # Retain list elements value items # Using filter() + lambda # initializing dictionary test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # initializing target list tar_list = [ 3 , 4 , 10 ] # Retain list elements value items # Using filter() + lambda res = dict ( filter ( lambda sub: sub[ 1 ] in tar_list, test_dict.items())) # printing result print ("The filtered dictionary : " + str (res)) |
The original dictionary is : {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'Lazyroar': 10} The filtered dictionary : {'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time complexity: O(n)
Auxiliary space: O(n)
Method 3: Using for loop
Use a simple for loop to iterate over the dictionary items and check if the value is present in the target list or not
Python3
# Python3 code to demonstrate working of # Retain list elements value items # Using for loop # Input test and target list test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } tar_list = [ 3 , 4 , 10 ] result_dict = {} for key, value in test_dict.items(): if value in tar_list: result_dict[key] = value # Printing filtered dictionary as asnwer print ( "The filtered dictionary : " + str (result_dict)) |
The filtered dictionary : {'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time complexity: O(n) where n is the size of the dictionary
Auxiliary space: O(m) where m is the size of the resultant dictionary
Method 4: Using intersection
We can use a set intersection to find the common values between the dictionary values and the target list. Then we can use dictionary comprehension to create a new dictionary with only the key-value pairs where the value is present in the common set.
Algorithm:
1. Convert ‘test_dict’ values to a set ‘dict_set’.
2. Create a new set ‘tar_set’ from ‘tar_list’.
3. Find the intersection of ‘dict_set’ and ‘tar_set’.
4. Create a new dictionary ‘new_dict’ using dictionary comprehension.
5. Filter the key-value pairs using the ‘if’ condition to check if the value is present in the intersection set.
6. Return ‘new_dict’.
Python3
def retain_dict_elements(test_dict, tar_list): dict_set = set (test_dict.values()) tar_set = set (tar_list) common_set = dict_set.intersection(tar_set) new_dict = {key: value for key, value in test_dict.items() if value in common_set} return new_dict test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } tar_list = [ 3 , 4 , 10 ] print (retain_dict_elements(test_dict, tar_list)) |
{'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time Complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary Space: O(m), where m is the number of key-value pairs in the new dictionary.
Approach #5: Using list comprehension and dictionary.fromkeys()
In this approach, we will use list comprehension to create a list of dictionary keys whose values are present in the target list. We will then use the dictionary.fromkeys() method to create a new dictionary with the selected keys and their corresponding values from the original dictionary.
Steps:
- Initialize the dictionary and target list.
- Use list comprehension to create a list of dictionary keys whose values are present in the target list.
- Use the dictionary.fromkeys() method to create a new dictionary with the selected keys and their corresponding values from the original dictionary.
- Store the resulting dictionary in a variable.
- Print the filtered dictionary.
Python3
# Python3 code to demonstrate working of # Retain list elements value items # Using list comprehension and dictionary.fromkeys() # Initializing dictionary test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } # Printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Initializing target list tar_list = [ 3 , 4 , 10 ] # Retaining the list elements value items # Using list comprehension and dictionary.fromkeys() res_keys = [key for key, value in test_dict.items() if value in tar_list] res = dict .fromkeys(res_keys, 0 ) # Looking for key in above result for key in res: res[key] = test_dict[key] # Printing the filtered dictionary as result print ( "The filtered dictionary : " + str (res)) |
The original dictionary is : {'gfg': 3, 'is': 2, 'best': 4, 'for': 7, 'Lazyroar': 10} The filtered dictionary : {'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time complexity: O(n)
Auxiliary space: O(n)
Approach 6 : Using set comprehension
- Initialize the dictionary and the target list, similar to the existing code.
- Use a set comprehension to create a new set that contains the keys in the dictionary whose values are in the target list.
- Create a new dictionary using a dictionary comprehension that includes only the keys in the new set.
- Return the new dictionary.
Python3
# Initializing dictionary test_dict = { 'gfg' : 3 , 'is' : 2 , 'best' : 4 , 'for' : 7 , 'Lazyroar' : 10 } # Initializing target list tar_list = [ 3 , 4 , 10 ] # Retaining the list elements value items # Using set comprehension key_set = {key for key, value in test_dict.items() if value in tar_list} res = {key: test_dict[key] for key in key_set} # Printing the filtered dictionary as result print ( "The filtered dictionary : " + str (res)) |
The filtered dictionary : {'gfg': 3, 'best': 4, 'Lazyroar': 10}
Time complexity: O(n), where n is the size of the dictionary.
Space complexity: O(m), where m is the number of keys that match the target values.