Given list of dictionaries, remove dictionary whose Key K is N.
Input : test_list = [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}], K = “Gfg”, N = 9
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}]
Explanation : All elements are extracted which have “Gfg” other than 9.
Input : test_list = [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 15}], K = “Best”, N = 10
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 3, “is” : 6, “Best” : 15}]
Explanation : All elements are extracted which have “Best” other than 10.
Remove dictionary if given key’s value Using list comprehension
This is one of the ways in which this task can be performed. In this, we extract and iterate using conditional checks using list comprehension in one liner.
Python3
test_list = [{ "Gfg" : 3 , "is" : 7 , "Best" : 8 },
{ "Gfg" : 9 , "is" : 2 , "Best" : 9 },
{ "Gfg" : 5 , "is" : 4 , "Best" : 10 },
{ "Gfg" : 3 , "is" : 6 , "Best" : 8 }]
print ( "The original list : " + str (test_list))
K = "Gfg"
N = 5
res = [sub for sub in test_list if sub[K] ! = N]
print ( "The extracted dictionaries : " + str (res))
|
OutputThe original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted dictionaries : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 3, 'is': 6, 'Best': 8}]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Remove dictionary if given key’s value Using filter() + lambda
This is yet another way in which this task can be performed. In this, we use conditionals using filter() and lambda function is for checking for N value.
Python3
test_list = [{ "Gfg" : 3 , "is" : 7 , "Best" : 8 },
{ "Gfg" : 9 , "is" : 2 , "Best" : 9 },
{ "Gfg" : 5 , "is" : 4 , "Best" : 10 },
{ "Gfg" : 3 , "is" : 6 , "Best" : 8 }]
print ( "The original list : " + str (test_list))
K = "Gfg"
N = 5
res = list ( filter ( lambda x: x[K] ! = N, test_list))
print ( "The extracted dictionaries : " + str (res))
|
OutputThe original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted dictionaries : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 3, 'is': 6, 'Best': 8}]
Remove dictionary if given key’s value Using a for loop
Use a for loop to iterate through the list of dictionaries and create a new list of dictionaries that do not have the key “K” equal to “N”.
Python3
test_list = [{ "Gfg" : 3 , "is" : 7 , "Best" : 8 },
{ "Gfg" : 9 , "is" : 2 , "Best" : 9 },
{ "Gfg" : 5 , "is" : 4 , "Best" : 10 },
{ "Gfg" : 3 , "is" : 6 , "Best" : 8 }]
print ( "The original list : " + str (test_list))
K = "Gfg"
N = 5
res = []
for sub in test_list:
if K in sub and sub[K] ! = N:
res.append(sub)
print ( "The extracted dictionaries : " + str (res))
|
OutputThe original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted dictionaries : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 3, 'is': 6, 'Best': 8}]
Time complexity: O(n), where n is the number of dictionaries in the list.
Auxiliary space: O(m), where m is the number of dictionaries that satisfy the condition, i.e., have the key “K” and its value is not equal to “N”.