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
test_list = [{ 'Gfg' : 1 , 'Best' : 3 },
{ 'Gfg' : 3 , 'Best' : 5 },
{ 'Best' : 3 }]
print ("The original list : " + str (test_list))
K = 'Gfg'
res = [ele for ele in test_list if K in ele]
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
test_list = [{ 'Gfg' : 1 , 'Best' : 3 },
{ 'Gfg' : 3 , 'Best' : 5 },
{ 'Best' : 3 }]
print ("The original list : " + str (test_list))
K = 'Gfg'
res = [ele for ele in test_list if K in ele.keys()]
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
import operator as op
test_list = [{ 'Gfg' : 1 , 'Best' : 3 },
{ 'Gfg' : 3 , 'Best' : 5 },
{ 'Best' : 3 }]
print ( "The original list : " + str (test_list))
K = 'Gfg'
res = [ele for ele in test_list if op.countOf(ele, K) > 0 ]
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
test_list = [{ 'Gfg' : 1 , 'Best' : 3 },
{ 'Gfg' : 3 , 'Best' : 5 },
{ 'Best' : 3 }]
print ( "The original list : " + str (test_list))
K = 'Gfg'
res = list ( filter ( lambda ele: K in ele, test_list))
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
test_list = [{ 'Gfg' : 1 , 'Best' : 3 },
{ 'Gfg' : 3 , 'Best' : 5 },
{ 'Best' : 3 }]
K = 'Gfg'
new_list = []
for d in test_list:
if K in d.keys():
new_list.append(d)
print ( "The original list : " + str (test_list))
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.
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 - 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 | Mutually different Records
Sometimes, while working with data, we may have a problem in which we require to find the unmatching records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
4 min read
Python - Remove empty List from List
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. [GFGTABS] Python a
2 min read
Python - Remove Multiple Keys from Dictionary
We are given a dictionary and our task is to remove multiple keys from the dictionary. For example, consider a dictionary d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} where we want to remove the keys 'b' and 'd', then the output will be {'a': 1, 'c': 3}. Let's explore different methods to remove multiple ke
3 min read