Remove None Nested Records in Python

In this tutorial, we will be learning how to remove none nested records in Python. We sometimes need to remove the data which have all key’s values as Null/None in nested records. This kind of situation can arise while pre-processing the data.

There are two ways in which we can complete the above task:

  1. Using any() + dictionary comprehension
  2. Utilising all() + dictionary comprehension

Python program to remove none nested records

Using any() + dictionary comprehension

Firstly, we will create a dictionary with some keys and it’s values and in the second line, we will print the dictionary

dict_data = {'Codespeedy' : {'abc' : 5, 'bcd' : 7},  
              'the' : {'def' : None, 'efg' : None},  
              'great' : {'ghi' : 1}}

print("The original dictionary is : " + str(dict_data))
Output of the above code is:
The original dictionary is : {'Codespeedy': {'abc': 5, 'bcd': 7}, 'the': {'def': None, 'efg': None}, 'great': {'ghi': 1}}

Now, we will write the Python function which will remove the none nested records:

output = {key: sub1 for key, sub1 in dict_data.items() if
      any(sub2 is None for sub2 in sub1.values())}

print("The dictionary after removal : " + str(output))

The output of the above code block is:

The dictionary after removal : {'Codespeedy': {'abc': 5, 'bcd': 7}, 'great': {'ghi': 1}}

Entire Code

dict_data = {'Codespeedy' : {'abc' : 5, 'bcd' : 7},  
              'the' : {'def' : None, 'efg' : None},  
              'great' : {'ghi' : 1}} 

print("The original dictionary is : " + str(dict_data )) 

output = {key: sub1 for key, sub1 in dict_data .items() if
      any(sub2 is None for sub2 in sub1.values())}

print("The dictionary after removal : " + str(output))

The second way in which we can obtain a similar kind of result is by:

Using all() + dictionary comprehension

Similar to the above code, we will first create a test dictionary and print it’s output:

dict_data = {'Codespeedy' : {'abc' : 5, 'bcd' : 7},  
              'the' : {'def' : None, 'efg' : None},  
              'great' : {'ghi' : 1}}

print("The original dictionary is : " + str(dict_data))
The original dictionary is : {'Codespeedy': {'abc': 5, 'bcd': 7}, 'the': {'def': None, 'efg': None}, 'great': {'ghi': 1}}

Now, we will write the function using all() and the dictionary comprehension:

output = {key: sub1 for key, sub1 in dict_data.items() if not
      all(sub2 is None for sub2 in sub1.values())}

print("The dictionary after removal : " + str(output))

The output of the above code will be:

The dictionary after removal : {'gfg': {'a': 1, 'b': 2}, 'best': {'g': 1}}

Entire Code

dict_data = {'Codespeedy' : {'abc' : 5, 'bcd' : 7},  
              'the' : {'def' : None, 'efg' : None},  
              'great' : {'ghi' : 1}} 

print("The original dictionary is : " + str(dict_data)) 

output = {key: sub1 for key, sub1 in test_dict.items() if not
      all(sub2 is None for sub2 in sub1.values())} 
  
print("The dictionary after removal : " + str(output))

If you want to refer topics related to the dictionary, you can refer to these sites:

Leave a Reply

Your email address will not be published. Required fields are marked *