Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Test for empty Nested Records

  • Last Updated : 14 May, 2020

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 discuss certain way in which this task can be performed.

Input : test_dict = {‘Gfg’: [], ‘geeks’: {}}
Output : True

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Input : test_dict = {‘Gfg’: 4}
Output : False

Method #1 : Using recursion + all() + isinstance()
The combination of above functionalities can be used to solve this problem. In this, we check for all nesting using all(), recursion and isinstance() is used to test for dictionary or list.

# Python3 code to demonstrate working of 
# Test for empty Nested Records
# Using recursion + all() + isinstance
# Helper function
def hlper_fnc(test_dict):
    if isinstance(test_dict, dict):
        return all(hlper_fnc(sub) for _, sub in test_dict.items())
    if isinstance(test_dict, list):
        return all(hlper_fnc(sub) for sub in test_dict)
    return False
# initializing dictionary
test_dict = {'Gfg': [], 'is': { 'best': [], 'for': {} }, 'geeks': {}}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# Test for empty Nested Records
# Using recursion + all() + isinstance
res = hlper_fnc(test_dict)
# printing result 
print("Is dictionary without data ? : " + str(res)) 
Output :

The original dictionary : {‘is’: {‘best’: [], ‘for’: {}}, ‘geeks’: {}, ‘Gfg’: []}
Is dictionary without data ? : True

My Personal Notes arrow_drop_up
Recommended Articles
Page :