Python | Check if all values are 0 in dictionary
While working with dictionary, we might come to a problem in which we require to ensure that all the values are 0 in dictionary. This kind of problem can occur while checking status of start or checking for a bug/action that could have occurred. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using all() + dictionary comprehension
The combination of above functions can be used to perform the following task. The all function checks for each key and dictionary comprehension checks for the 0 value.
# Python3 code to demonstrate working of # Check if all values are 0 in dictionary # Using all() + dictionary comprehension # Initialize dictionary test_dict = {'gfg' : 0, 'is' : 0, 'best' : 0} # Printing original dictionary print("The original dictionary is : " + str(test_dict)) # using all() + dictionary comprehension # Check if all values are 0 in dictionary res = all(x == 0 for x in test_dict.values()) # printing result print("Does all keys have 0 value ? : " + str(res)) |
The original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Method #2 : Using any() + not operator
The combination of above functions can be used to perform this particular task. Rather than checking for all 0, we check for any one non zero value, and negate the result.
# Python3 code to demonstrate working of # Check if all values are 0 in dictionary # Using any() + not operator # Initialize dictionary test_dict = {'gfg' : 0, 'is' : 1, 'best' : 0} # Printing original dictionary print("The original dictionary is : " + str(test_dict)) # using any() + not operator # Check if all values are 0 in dictionary res = not any(test_dict.values()) # printing result print("Does all keys have 0 value ? : " + str(res)) |
The original dictionary is : {'gfg': 0, 'is': 1, 'best': 0}
Does all keys have 0 value ? : False
Recommended Posts:
- Python | Check for None values in given dictionary
- Python dictionary | values()
- Python | Selective key values in dictionary
- Python | N largest values in dictionary
- Python | Sum values for each key in nested dictionary
- Python | Initialize dictionary with None values
- Python | Get Unique values from list of dictionary
- Python | Grouping list values into dictionary
- Map function and Dictionary in Python to sum ASCII values
- Python | Summation of dictionary list values
- Python | Create a dictionary using list with none values
- Python | Type conversion in dictionary values
- Python | Filter the negative values from given dictionary
- Python | Combining values from dictionary of list
- Python | Find keys with duplicate values in dictionary
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.



