Open In App
Related Articles

Python – Test for Empty Dictionary Value List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 :

  • Use the any() function with the values() method of the dictionary test_dict to check if any of the values in the dictionary are empty.
  • Use the not keyword to negate the result of the any() function.
  • Print the result with a message saying “Are value lists empty? : ” and the value of res.

Below is the implementation of the above approach:

Python3

# 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

# 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

# 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:

  • Initialize a dictionary called “test_dict” with keys and empty value lists.
  • Print the original dictionary.
  • Convert the dictionary values to a list called “x”.
  • Use the count() method from the operator module to count the number of empty lists in “x”.
  • Compare the number of empty lists with the length of “x”.
  • If the number of empty lists is equal to the length of “x”, set the value of “res” to True, else keep it as False.
  • Print the result whether the value lists are empty or not.

Below is the implementation of the above approach:

Python3

# 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

# 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

# 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. 



Last Updated : 10 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads