Python - Remove Dictionaries with Matching Values with K Key
Last Updated :
21 Apr, 2023
Given two dictionary lists, remove all the dictionaries which have similar value of K key from other dictionary list.
Input : test_list = [{'Gfg' : 3, "is" : 3, "best" : 9},
{'Gfg' : 8, "is" : 4, "best" : 2},
{'Gfg' : 9, "is" : 2, "best" : 4},
{'Gfg' : 8, "is" : 10, "best" : 3},
{'Gfg' : 7, "is" : 1, "best" : 7}], check_list = [{'Gfg' : 8, "Best" : 1}, {"Best" : 2, "Gfg" : 7}], K = "Gfg"
Output : [{'Gfg': 3, 'is': 3, 'best': 9}, {'Gfg': 9, 'is': 10, 'best': 3}]
Explanation : All dictionaries with "Gfg"'s value as 8 or 7 is removed.
Input : test_list = [{'Gfg' : 3, "is" : 3, "best" : 9},
{'Gfg' : 8, "is" : 4, "best" : 2}], check_list = [{'Gfg' : 8, "Best" : 1}, {"Best" : 2, "Gfg" : 7}], K = "Gfg"
Output : [{'Gfg': 3, 'is': 3, 'best': 9}]
Explanation : All dictionaries with "Gfg"'s value as 8 or 7 is removed.
Method 1: Using list comprehension + dictionary comprehension
In this, we perform tasks of getting a set of elements using dictionary comprehension, and then a new list is constructed using list comprehension by testing K key's values absence in the constructed set of values.
The whole task is conducted in two steps,
- All the unique values are extracted from check_list corresponding to key K using set().
- Each dictionary in test list is checked for key K's value, if it is present in set created in first step, then that dictionary is omitted in result, else dictionary is added as result.
Python3
# Python3 code to demonstrate working of
# Remove Dictionaries with Matching Values with K Key
# Using dictionary comprehension + list comprehension
# initializing list
test_list = [{'Gfg': 3, "is": 3, "best": 9},
{'Gfg': 8, "is": 4, "best": 2},
{'Gfg': 1, "is": 2, "best": 4},
{'Gfg': 9, "is": 10, "best": 3},
{'Gfg': 7, "is": 1, "best": 7}]
# printing original list
print("The original list is : " + str(test_list))
# initializing check dictionary list
check_list = [{'Gfg': 8, "Best": 1}, {"Best": 2, "Gfg": 7}]
# initializing Key
K = "Gfg"
# getting set of values
temp = {sub[K] for sub in check_list}
# checking for value occurrence in test dictionary using in
# filtering only with no matching values
res = [sub for sub in test_list if sub[K] not in temp]
# printing result
print("Dictionary list after removal : " + str(res))
Output:
The original list is : [{'Gfg': 3, 'is': 3, 'best': 9}, {'Gfg': 8, 'is': 4, 'best': 2}, {'Gfg': 1, 'is': 2, 'best': 4}, {'Gfg': 9, 'is': 10, 'best': 3}, {'Gfg': 7, 'is': 1, 'best': 7}] Dictionary list after removal : [{'Gfg': 3, 'is': 3, 'best': 9}, {'Gfg': 1, 'is': 2, 'best': 4}, {'Gfg': 9, 'is': 10, 'best': 3}]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 : Using filter() function and lambda function
- Initialize the original list, test_list.
- Initialize the check dictionary list, check_list.
- Initialize the Key, K.
- Create a set of values from the Key in the check dictionary list.
- Use the filter() function with a lambda function that checks if the value of the Key in the dictionary is not in the set of values created in step 4. This will filter out all the dictionaries that have a value of K that matches with the set of values we created in step 4.
- Create a list from the filtered objects returned by the filter() function. This list will contain all the dictionaries that do not have a matching value for the Key K in the check_list.
Python3
# initializing list
test_list = [{'Gfg': 3, "is": 3, "best": 9},
{'Gfg': 8, "is": 4, "best": 2},
{'Gfg': 1, "is": 2, "best": 4},
{'Gfg': 9, "is": 10, "best": 3},
{'Gfg': 7, "is": 1, "best": 7}]
# initializing check dictionary list
check_list = [{'Gfg': 8, "Best": 1}, {"Best": 2, "Gfg": 7}]
# initializing Key
K = "Gfg"
# getting set of values
temp = {sub[K] for sub in check_list}
# using filter() function with lambda function
res = list(filter(lambda x: x[K] not in temp, test_list))
# printing result
print("Dictionary list after removal : " + str(res))
OutputDictionary list after removal : [{'Gfg': 3, 'is': 3, 'best': 9}, {'Gfg': 1, 'is': 2, 'best': 4}, {'Gfg': 9, 'is': 10, 'best': 3}]
Time complexity: O(n), where n is the number of dictionaries in the original list, test_list.
Auxiliary space: O(m), where m is the number of dictionaries in the check_list.
Method 3: Using a for loop and a new list to store the desired output.
Here are the steps:
- Initialize the list result_list to an empty list.
- Loop through each dictionary in the test_list.
- Check if the value of the key K is in any of the dictionaries in the check_list.
- If it is not in any of the dictionaries, append the dictionary to the result_list.
- Print the result_list.
Python3
# initializing list
test_list = [{'Gfg': 3, "is": 3, "best": 9},
{'Gfg': 8, "is": 4, "best": 2},
{'Gfg': 1, "is": 2, "best": 4},
{'Gfg': 9, "is": 10, "best": 3},
{'Gfg': 7, "is": 1, "best": 7}]
# initializing check dictionary list
check_list = [{'Gfg': 8, "Best": 1}, {"Best": 2, "Gfg": 7}]
# initializing Key
K = "Gfg"
# initializing result list
result_list = []
# iterating through each dictionary in test_list
for dictionary in test_list:
# checking if K is not in any of the dictionaries in check_list
if not any(K in d and d[K] == dictionary[K] for d in check_list):
result_list.append(dictionary)
# printing result
print("Dictionary list after removal : " + str(result_list))
OutputDictionary list after removal : [{'Gfg': 3, 'is': 3, 'best': 9}, {'Gfg': 1, 'is': 2, 'best': 4}, {'Gfg': 9, 'is': 10, 'best': 3}]
The time complexity of this method is O(n*m), where n is the length of test_list and m is the length of check_list.
The auxiliary space complexity is O(k), where k is the length of result_list.
Similar Reads
Remove K Value Items from Dictionary Nesting - Python We are given a dictionary we need to remove K value items from dictionary. For example we are having a dictionary d = {'a': {'b': 'remove', 'c': 'keep'}, 'd': {'e': 'remove', 'f': 'keep'}} we need to remove the K value suppose in this case K is 'remove' so that output should be {'a': {'c': 'keep'},
3 min read
Python - Remove duplicate values in dictionary Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the removal of all the duplicate values of dictionary, and we are not concerned if any key get removed in the process. This kind of application can occur in school programming and day-day programming.
8 min read
Python - Remove Dictionary if Given Key's Value is N We are given a dictionary we need to remove key if the given value of key is N. For example, we are given a dictionary d = {'a': 1, 'b': 2, 'c': 3} we need to remove the key if the value is N so that the output becomes {'a': 1, 'c': 3}. We can use methods like del, pop and various other methods like
2 min read
Remove Keys from Dictionary Starting with K - Python We are given a dictionary we need to remove the Keys which are starting with K. For example we are give a dictionary d = {'Key1': 'value1', 'Key2': 'value2', 'other_Key': 'value3'} so that the output should be {'other_Key': 'value3'}Using Dictionary ComprehensionUsing dictionary comprehension we can
3 min read
Remove Keys from Dictionary Starting with K - Python We are given a dictionary we need to remove the Keys which are starting with K. For example we are give a dictionary d = {'Key1': 'value1', 'Key2': 'value2', 'other_Key': 'value3'} so that the output should be {'other_Key': 'value3'}Using Dictionary ComprehensionUsing dictionary comprehension we can
3 min read
Python - Remove K valued key from Nested Dictionary We are given a nested dictionary we need to remove K valued key. For example, we are given a nested dictionary d = { "a": 1, "b": {"c": 2,"d": {"e": 3,"f": 1},"g": 1},"h": [1, {"i": 1, "j": 4}]} we need to remove K valued key ( in our case we took k value as 1 ) from it so that the output should be
3 min read