Open In App

Python – Remove dictionary if given key’s value is N

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using list comprehension
 
# initializing list
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}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "Gfg"
 
# initializing N
N = 5
 
# returning only dictionaries with "Gfg" key not 5
res = [sub for sub in test_list if sub[K] != N]
 
# printing result
print("The extracted dictionaries : " + str(res))


Output

The 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




# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using filter() + lambda
 
# initializing list
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}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "Gfg"
 
# initializing N
N = 5
 
# Using filter() to check for N value
res = list(filter(lambda x: x[K] != N, test_list))
 
# printing result
print("The extracted dictionaries : " + str(res))


Output

The 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




# Python3 code to demonstrate working of
# Remove Dictionaries whose Key(K) is N
# Using for loop
 
# initializing list
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}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = "Gfg"
 
# initializing N
N = 5
 
# initialize an empty list for the result
res = []
 
# iterate through each dictionary in the list
for sub in test_list:
    # check if the dictionary has the key "K" and its value is not equal to "N"
    if K in sub and sub[K] != N:
        # if so, append it to the result list
        res.append(sub)
 
# printing result
print("The extracted dictionaries : " + str(res))


Output

The 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”.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads