Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract all the values in values lists that match the filtered indices from matching values from list with some key in dictionary. This kind of application can occur in web development.
Input : test_dict = {“Gfg” : [4, 5, 7], “is” : [5, 6, 8], “best” : [10, 7, 4]} Output : {“Gfg” : [4, 5, 7], “is” : [5, 6, 8], “best” : [10, 7, 4]} Input : test_dict = {“Gfg” : [4, 20, 5, 7], “is” : [5, 17, 6, 8], “best” : [10, 11, 7, 4]} Output : {‘Gfg’: [4, 5, 7], ‘is’: [5, 6, 8], ‘best’: [10, 7, 4]}
Method #1 : Using loop + zip() + defaultdict() The combination of above methods can be used to solve this problem. In this, we initialize the defaultdict with list, bind all the keys with zip() and use loop to append the required elements.
Python3
# Python3 code to demonstrate working of # Filter index similar values # Using loop + zip() + defaultdict() from collections import defaultdict # initializing dictionary test_dict = {"Gfg" : [ 1 , 4 , 5 , 6 , 7 ], " is " : [ 5 , 6 , 8 , 9 , 10 ], "best" : [ 10 , 7 , 4 , 11 , 23 ]} # printing original dictionary print ("The original dictionary : " + str (test_dict)) # initializing value list filt_list = [ 4 , 5 , 7 ] # Filter index similar values # Using loop + zip() + defaultdict() res = defaultdict( list ) for x, y, z in zip (test_dict[ 'Gfg' ], test_dict[ 'is' ], test_dict[ 'best' ]): if x in filt_list: res[ 'Gfg' ].append(x) res[ 'is' ].append(y) res[ 'best' ].append(z) # printing result print ("The filtered dictionary : " + str ( dict (res))) |
The original dictionary : {‘Gfg’: [1, 4, 5, 6, 7], ‘is’: [5, 6, 8, 9, 10], ‘best’: [10, 7, 4, 11, 23]} The filtered dictionary : {‘Gfg’: [4, 5, 7], ‘is’: [6, 8, 10], ‘best’: [7, 4, 23]}
Method #2 : Using list comprehension + dictionary comprehension The combination of above functionalities can be used to solve this problem. In this, we extract indices using list comprehension and filtering from other keys is done using dictionary comprehension.
Python3
# Python3 code to demonstrate working of # Filter index similar values # Using list comprehension + dictionary comprehension # initializing dictionary test_dict = {"Gfg" : [ 1 , 4 , 5 , 6 , 7 ], " is " : [ 5 , 6 , 8 , 9 , 10 ], "best" : [ 10 , 7 , 4 , 11 , 23 ]} # printing original dictionary print ("The original dictionary : " + str (test_dict)) # initializing value list filt_list = [ 4 , 5 , 7 ] # Filter index similar values # Using list comprehension + dictionary comprehension temp = [test_dict[ 'Gfg' ].index(idx) for idx in filt_list] res = {key : [test_dict[key][idx] for idx in temp] for key in test_dict.keys()} # printing result print ("The filtered dictionary : " + str (res)) |
The original dictionary : {‘Gfg’: [1, 4, 5, 6, 7], ‘is’: [5, 6, 8, 9, 10], ‘best’: [10, 7, 4, 11, 23]} The filtered dictionary : {‘Gfg’: [4, 5, 7], ‘is’: [6, 8, 10], ‘best’: [7, 4, 23]}
Using a set to store the unique values:
Approach:
Create an empty set to store unique values.
Iterate through the values in the dictionary.
Convert each value to a tuple to make it hashable.
Check if the tuple is already in the set of unique values.
If the tuple is already in the set, remove the current key-value pair from the dictionary.
If the tuple is not in the set, add it to the set of unique values.
Return the filtered dictionary.
Python3
def filter_dict(test_dict): unique_values = set () for value in test_dict.values(): unique_value = tuple (value) if unique_value in unique_values: del test_dict[key] else : unique_values.add(unique_value) return test_dict test_dict = { "Gfg" : [ 4 , 5 , 7 ], "is" : [ 5 , 6 , 8 ], "best" : [ 10 , 7 , 4 ]} print (filter_dict(test_dict)) # Output: {'Gfg': [4, 5, 7], 'is': [5, 6, 8], 'best': [4]} |
{'Gfg': [4, 5, 7], 'is': [5, 6, 8], 'best': [10, 7, 4]}
Time complexity: O(n)
Auxiliary Space: O(n)