Python - Remove records if Key not present
Last Updated :
13 Mar, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Remove None Nested Records
Sometimes, while working with Python Records, can have problem in which we need to perform the removal of data which have all key's values as None in nested records. This kind of problem can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method
4 min read
Python - Remove K from Records
Sometimes, while working with Python tuples, we can have a problem in which we need to remove all K from lists. This task can have application in many domains such as web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [(5, 6,
5 min read
Python - Remove Record if Nth Column is K
Sometimes while working with a list of records, we can have a problem in which we need to perform the removal of records on the basis of the presence of certain elements at the Nth position of the record. Let us discuss certain ways in which this task can be performed. Method #1: Using loop This is
10 min read
Python - Remove nested records from tuple
Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Letâs discuss certain way in which this task c
5 min read
Python - Remove Consecutive K element records
Sometimes, while working with Python records, we can have a problem in which we need to remove records on the basis of presence of consecutive K elements in tuple. This kind of problem is peculiar but can have applications in data domains. Let's discuss certain ways in which this task can be perform
7 min read
Python - Test for empty Nested Records
Sometimes, while working with Python dictionaries, we can have a problem in which we need to test if a particular dictionary has nested records, and all of them is empty, i.e with no key or no value in case of list. This kind of problem is quite common in data domains such as Data Science. Let's dis
6 min read
Python - Replace Non-Maximum Records
Sometimes, while working with Python records, we can have a problem in which we need to perform replace of all the records whose one element is not Maximum. This kind of problem can have application in many domains including day-day programming and web development domain. Let's discuss certain ways
4 min read
Python - Remove all duplicate occurring tuple records
Sometimes, while working with records, we can have a problem of removing those records which occur more than once. This kind of application can occur in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() Init
6 min read
Python - Remove List Item
Removing List Item can be achieved using several built-in methods that provide flexibility in how you remove list items. In this article, we'll explore the different ways to remove list items in Python.Removing Item by Value with remove()The remove() method allows us to remove the first occurrence o
3 min read
Remove empty Lists from List - Python
In this article, we will explore various method to remove empty lists from a list. The simplest method is by using a for loop.Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list.Pythona = [[1, 2], [
2 min read