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.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.