Python - Remove None Nested Records
Last Updated :
27 Apr, 2023
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 #1 : Using all() + dictionary comprehension The combination of above functionalities can be used to solve this problem. In this, the removal of all the values with all None values are not included in dictionary re construction are done using all().
Python3
# Python3 code to demonstrate working of
# Remove None Nested Records
# Using all() + dictionary comprehension
# initializing dictionary
test_dict = {'gfg' : {'a' : 1, 'b' : 2},
'is' : {'d' : None, 'e' : None},
'best' : {'g' : 1}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove None Nested Records
# Using all() + dictionary comprehension
res = {key: sub1 for key, sub1 in test_dict.items() if not
all(sub2 is None for sub2 in sub1.values())}
# printing result
print("The dictionary after removal : " + str(res))
Output :
The original dictionary is : {'gfg': {'b': 2, 'a': 1}, 'is': {'e': None, 'd': None}, 'best': {'g': 1}} The dictionary after removal : {'gfg': {'b': 2, 'a': 1}, 'best': {'g': 1}}
Time Complexity: O(n*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
Method #2 : Using any() + dictionary comprehension The combination of above functions can also be used to solve this problem. In this, we keep all the records which have any key as not none using any().
Python3
# Python3 code to demonstrate working of
# Remove None Nested Records
# Using any() + dictionary comprehension
# initializing dictionary
test_dict = {'gfg' : {'a' : 1, 'b' : 2},
'is' : {'d' : None, 'e' : None},
'best' : {'g' : 1}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove None Nested Records
# Using any() + dictionary comprehension
res = {key: sub1 for key, sub1 in test_dict.items() if
any(sub2 is not None for sub2 in sub1.values())}
# printing result
print("The dictionary after removal : " + str(res))
Output :
The original dictionary is : {'gfg': {'b': 2, 'a': 1}, 'is': {'e': None, 'd': None}, 'best': {'g': 1}} The dictionary after removal : {'gfg': {'b': 2, 'a': 1}, 'best': {'g': 1}}
Time complexity: O(n*m), where n is the number of keys in the dictionary and m is the maximum number of key-value pairs in any sub-dictionary.
Auxiliary space: O(n*m), a new temporary dictionary for each sub-dictionary and add only the non-None key-value pairs to it
Method 3: Using a for loop and conditional statements:
This method loops over the keys and values of the dictionary using two for loops. For each sub-dictionary, it creates a new temporary dictionary temp_dict and adds only those key-value pairs that have non-None values. If the temporary dictionary is not empty, it is added to the result dictionary res.
Python3
# initializing dictionary
test_dict = {'gfg' : {'a' : 1, 'b' : 2},
'is' : {'d' : None, 'e' : None},
'best' : {'g' : 1}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove None Nested Records
# Using for loop and conditional statements
res = {}
for key, sub_dict in test_dict.items():
temp_dict = {}
for sub_key, sub_value in sub_dict.items():
if sub_value is not None:
temp_dict[sub_key] = sub_value
if temp_dict:
res[key] = temp_dict
# printing result
print("The dictionary after removal : " + str(res))
OutputThe original dictionary is : {'gfg': {'a': 1, 'b': 2}, 'is': {'d': None, 'e': None}, 'best': {'g': 1}}
The dictionary after removal : {'gfg': {'a': 1, 'b': 2}, 'best': {'g': 1}}
Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum number of key-value pairs in any sub-dictionary.
Auxiliary space: O(nm), because we create a new temporary dictionary for each sub-dictionary and add only the non-None key-value pairs to it
Similar Reads
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 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 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 - Remove records if Key not present 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 whi
6 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