Open In App

Python – Test for Empty Dictionary Value List

Given a dictionary with list as values, check if all lists are empty.

Input : {“Gfg” : [], “Best” : []} 
Output : True 
Explanation : Both lists have no elements, hence True. 



Input : {“Gfg” : [], “Best” : [4]} 
Output : False 
Explanation : “Best” contains element, Hence False.

Method #1 : Using any() + values()



The combination of above functions can be used to solve this  task. In this, we check for any value present in values we extract using values(), if not found, then list is empty.

Step-by-step approach :

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
# Using any() + values()
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# checking if any value is found
# using not to negate the result of any()
res = not any(test_dict.values())
 
# printing result
print("Are value lists empty? : " + str(res))

Output
The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1), as only constant amount of extra space is required to store the intermediate result and the original dictionary.

Method #2 : Using all() + values()

This is another way in which we can solve the problem. In this, we can check for each key if all values are empty using all().




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
# Using all() + values()
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# checking if all keys have empty list 
res = all(ele == [] for ele in list(test_dict.values()))
 
# printing result
print("Are value lists empty? : " + str(res))

Output
The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(1), as the size of the dictionary and the boolean variable ‘res’ do not depend on the size of the input dictionary.

Method #3: Using values(),count() and len() methods




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# checking if any value is found
x=list(test_dict.values())
res=False
if(x.count([])==len(x)):
    res=True
# printing result
print("Are value lists empty? : " + str(res))

Output :

The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method #4 :  Using values(),operator.countOf() and len() methods

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# checking if any value is found
x=list(test_dict.values())
res=False
import operator
if(operator.countOf(x,[])==len(x)):
    res=True
# printing result
print("Are value lists empty? : " + str(res))

Output
The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #5: Using a for loop to iterate over the values of the dictionary

Initialize res to True assuming all value lists are empty. Then, we iterate over the values of the dictionary using a for loop, and if any value is found to be non-empty, we set res to False and break the loop. Finally, we print the result.




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using a for loop to iterate over the values of the dictionary
# if any value is not empty, set res to False and break the loop
res = True
for value in test_dict.values():
    if value:
        res = False
        break
 
# printing result
print("Are value lists empty? : " + str(res))

Output
The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time Complexity : O(N)
Auxiliary Space : O(1)

Method #6: Using the any() method with a generator expression

This approach checks if any of the values in the dictionary are not empty by creating a generator expression that yields True if the value is empty, and then applying the any() method to the resulting iterable. The not keyword is used to invert the resulting Boolean value so that the variable res is True if all the values are empty, and False otherwise.




# Python3 code to demonstrate working of
# Test for Empty Dictionary Value List
 
# initializing dictionary
test_dict = {"Gfg" : [], "Best" : [], "is" : []}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using any() with a generator expression to check if any value is not empty
res = not any(test_dict.values())
 
# printing result
print("Are value lists empty? : " + str(res))

Output
The original dictionary is : {'Gfg': [], 'Best': [], 'is': []}
Are value lists empty? : True

Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(1). The amount of extra space used by this algorithm does not depend on the size of the input. 


Article Tags :