Open In App

Python – Remove records if Key not present

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove all the dictionaries in which a particular key is not present. This kind of problem can have applications in many domains such as day-day programming and data domain. Let’s discuss certain ways in which this task can be performed.
 

Input : test_list = [{‘Gfg’ : 1, ‘Best’ : 3}, {‘Gfg’ : 3, ‘Best’ : 5}, {‘Best’ : 3}] , K = ‘Best’ 
Output : [{‘Gfg’ : 1, ‘Best’ : 3}, {‘Gfg’ : 3, ‘Best’ : 5}, {‘Best’ : 3}]

Input : test_list = [{‘Gfg’ : 1, ‘Best’ : 3}, {‘Gfg’ : 3, ‘Best’ : 5}, {‘Best’ : 3}], K = ‘good’ 
Output : [] 

Method #1: Using list comprehension 
This is one of the ways in which this task can be performed. In this, we iterate and test for key presence using list comprehension and conditional statements.

Python3




# Python3 code to demonstrate working of
# Remove records if Key not present
# Using list comprehension
 
# initializing list
test_list = [{'Gfg' : 1, 'Best' : 3},
             {'Gfg' : 3, 'Best' : 5},
             {'Best' : 3}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K Key
K = 'Gfg'
 
# Remove records if Key not present
# Using list comprehension
res = [ele for ele in test_list if K in ele]
         
# printing result
print("List after filtration : " + str(res))


Output : 

The original list : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}, {'Best': 3}]
List after filtration : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}]

 

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 in the list that contain the key ‘Gfg’. This is because the list comprehension creates a new list containing only the dictionaries that contain the key ‘Gfg’. The space complexity does not depend on the size of the dictionaries.

Method #2 : Using list comprehension + keys() 
The combination of the above functions can be used to solve this problem. In this, we perform the task of extraction of all the keys using keys(), reduces the overhead of checking in items.

Python3




# Python3 code to demonstrate working of
# Remove records if Key not present
# Using list comprehension + keys()
 
# initializing list
test_list = [{'Gfg' : 1, 'Best' : 3},
             {'Gfg' : 3, 'Best' : 5},
             {'Best' : 3}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K Key
K = 'Gfg'
 
# Remove records if Key not present
# Using list comprehension + keys()
res = [ele for ele in test_list if K in ele.keys()]
         
# printing result
print("List after filtration : " + str(res))


Output : 

The original list : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}, {'Best': 3}]
List after filtration : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}]

 

Time complexity: O(nm), where n is the number of dictionaries in the list and m is the average number of keys in each dictionary.
Auxiliary space complexity: O(n), where n is the number of dictionaries in the list. 

Method #3: Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Remove records if Key not present
import operator as op
# initializing list
test_list = [{'Gfg': 1, 'Best': 3},
             {'Gfg': 3, 'Best': 5},
             {'Best': 3}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K Key
K = 'Gfg'
 
# Remove records if Key not present
# Using list comprehension and op.countOf()
res = [ele for ele in test_list if op.countOf(ele, K) > 0]
 
# printing result
print("List after filtration : " + str(res))


Output

The original list : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}, {'Best': 3}]
List after filtration : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using filter() function

This program removes all the records from a list of dictionaries where the specified key ‘K’ is not present. It uses the filter() function to create a new list of elements that satisfy the given condition.

Python3




# Python3 code to demonstrate working of
# Remove records if Key not present
# Using filter() function
 
# initializing list
test_list = [{'Gfg' : 1, 'Best' : 3},
             {'Gfg' : 3, 'Best' : 5},
             {'Best' : 3}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K Key
K = 'Gfg'
 
# Remove records if Key not present
# Using filter() function
res = list(filter(lambda ele: K in ele, test_list))
         
# printing result
print("List after filtration : " + str(res))


Output

The original list : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}, {'Best': 3}]
List after filtration : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}]

Time complexity: O(N), where N is the number of elements in the list.
Auxiliary space: O(N), as the resulting list res can have at most N element.

Method #5: Using a simple for loop and dictionary methods.

Initializes an empty list called new_list, and then loops through the dictionaries in the test_list. For each dictionary, it checks if the key K is present in the dictionary using the keys() method. If the key is present, the dictionary is added to the new_list.

Python3




# initializing list
test_list = [{'Gfg' : 1, 'Best' : 3},
             {'Gfg' : 3, 'Best' : 5},
             {'Best' : 3}]
 
# initializing K Key
K = 'Gfg'
 
# Remove records if Key not present
# Without using filter()
 
new_list = []
for d in test_list:
    if K in d.keys():
        new_list.append(d)
 
# printing original list
print("The original list : " + str(test_list))
 
# printing result
print("List after filtration : " + str(new_list))


Output

The original list : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}, {'Best': 3}]
List after filtration : [{'Gfg': 1, 'Best': 3}, {'Gfg': 3, 'Best': 5}]

Time complexity: O(nk), where n is the number of dictionaries in the list test_list, and k is the average number of keys in each dictionary. 
Auxiliary space: O(nk), since a new list is created to store the filtered dictionaries.



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

Similar Reads