Python – Remove dictionary from a list of dictionaries if a particular value is not present
Last Updated :
04 May, 2023
Given a list of dictionaries, remove all dictionaries which don’t have K as a value.
Examples:
Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 7
Output : [{'Gfg': 4, 'is': 8, 'best': 9}]
Explanation : Resultant dictionary doesn't contain 7 as any element.
Input : test_list = [{"Gfg" : 4, "is" : 7, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 7
Output : []
Explanation : All contain 7 as element in List.
Method #1 : Using loop + values()
This is one of the ways in which this task can be performed. In this, we perform the task of iterating for all the dictionaries using a loop and check for value presence using values().
Python3
test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
print ( "The original list : " + str (test_list))
K = 7
res = []
for sub in test_list:
if K not in list (sub.values()):
res.append(sub)
print ( "Filtered dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this, we extract all the values using one-liner using list comprehension. The values are extracted using values().
Python3
test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
print ( "The original list : " + str (test_list))
K = 7
res = []
res = [sub for sub in test_list if K not in list (sub.values())]
print ( "Filtered dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Method #3: Using filter() and lambda function:
The function takes a list of dictionaries test_list and a value K as input. If the test_list is empty, the function returns an empty list. If the first dictionary in test_list contains the value K, the function returns a list containing that dictionary concatenated with the result of calling remove_dict_without_K with the rest of the list and the same value of K. Otherwise, the function simply returns the result of calling remove_dict_without_K with the rest of the list and the same value of K.
Python3
def remove_dict_without_K(test_list, K):
if not test_list:
return []
elif K in test_list[ 0 ].values():
return [test_list[ 0 ]] + remove_dict_without_K(test_list[ 1 :], K)
else :
return remove_dict_without_K(test_list[ 1 :], K)
test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
print ( "The original list : " + str (test_list))
K = 7
res = list ( filter ( lambda x: K not in x.values(), test_list))
print ( "Filtered dictionaries : " + str (res))
|
Output
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Time complexity: O(nm), where n is the length of test_list and m is the average number of key-value pairs in each dictionary.
Auxiliary Space: O(nm), where n is the length of test_list and m is the average number of key-value pairs in each dictionary.
Method 4: Using dictionary comprehension method.
Steps:
- Initialize an empty dictionary to store the filtered dictionaries.
- Use a dictionary comprehension to iterate through each dictionary in the given list and filter out the dictionaries that have K as one of the values,
- Print the resulting dictionary of filtered dictionaries.
Python3
test_list = [{ "Gfg" : 4 , "is" : 8 , "best" : 9 },
{ "Gfg" : 5 , "is" : 8 , "best" : 1 },
{ "Gfg" : 3 , "is" : 7 , "best" : 6 },
{ "Gfg" : 3 , "is" : 7 , "best" : 5 }]
print ( "The original list : " + str (test_list))
K = 7
filtered_dict = { str (i): dict_val for i, dict_val in enumerate (
test_list) if K not in dict_val.values()}
print ( "Filtered dictionaries : " + str ( list (filtered_dict.values())))
|
Output
The original list : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}, {'Gfg': 3, 'is': 7, 'best': 6}, {'Gfg': 3, 'is': 7, 'best': 5}]
Filtered dictionaries : [{'Gfg': 4, 'is': 8, 'best': 9}, {'Gfg': 5, 'is': 8, 'best': 1}]
Time complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary space: O(n), as we are creating a new dictionary to store the filtered dictionaries.
Share your thoughts in the comments
Please Login to comment...