Python | Check if all values are 0 in dictionary
Last Updated :
27 Apr, 2023
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 a loop
Step-by-step approach:
- Initialize a boolean variable flag to True.
- Iterate over the values in the dictionary using a for loop.
- If any value is not 0, set the flag variable to False and break the loop.
- Print the result by checking the value of the flag variable.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of
# Check if all values are 0 in dictionary
# Using a loop
# Initialize dictionary
test_dict = {'gfg': 0, 'is': 0, 'best': 0}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using a loop
# Check if all values are 0 in dictionary
flag = all(value == 0 for value in test_dict.values())
# printing result
print("Does all keys have 0 value ? : " + str(flag))
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(1)
Method #2: 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
# 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))
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using any() + not operator The combination of the 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
# 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': 0, '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))
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Method #4: Using values(),len() and count()
Python3
# Python3 code to demonstrate working of
# Check if all values are 0 in dictionary
# Initialize dictionary
test_dict = {'gfg' : 0, 'is' : 0, 'best' : 0}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Check if all values are 0 in dictionary
res = False
x=list(test_dict.values())
if(x.count(0)==len(x)):
res=True
# printing result
print("Does all keys have 0 value ? : " + str(res))
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Method #5: Using * and len() method
Python3
# Python3 code to demonstrate working of
# Check if all values are 0 in dictionary
# Initialize dictionary
test_dict = {'gfg' : 0, 'is' : 0, 'best' : 0}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Check if all values are 0 in dictionary
res = False
x=list(test_dict.values())
a=[x[0]]*len(x)
if(a==x):
res=True
# printing result
print("Does all keys have 0 value ? : " + str(res))
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Method #6: Using reduce() & lambda
This code snippet uses the reduce() and lambda function to check if all the values in the dictionary are 0. The reduce() function applies a specified function to all the elements of an iterable and returns a single value. The lambda function is used to define an anonymous function with no name. This code snippet takes O(n) time where n is the number of elements in the dictionary as it iterates through each element in the dictionary. It takes O(1) space as it does not require any extra space for computation.
Python3
# Python3 code to demonstrate working of
# Check if all values are 0 in dictionary
# Using reduce() & lambda
# Importing reduce from functools
from functools import reduce
# Initialize dictionary
test_dict = {'gfg': 0, 'is': 0, 'best': 0}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using reduce() & lambda
# Check if all values are 0 in dictionary
res = reduce((lambda x, y: x + y), test_dict.values())
# printing result
print("Does all keys have 0 value ? : " + str(bool(res == 0)))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original dictionary is : {'gfg': 0, 'is': 0, 'best': 0}
Does all keys have 0 value ? : True
Time complexity: O(n) where n is the number of items in the dictionary. The test_dict.values() method is used to extract the values of the dictionary, which is then passed to the reduce() function along with a lambda function. The lambda function performs the addition operation on all the elements of the list, which is equivalent to summing all the values of the dictionary. The reduce() function applies the lambda function to the items of the list in a single pass, resulting in a time complexity of O(n) where n is the number of items in the dictionary.
Auxiliary Space: O(1) as the reduce function performs the operation in-place and doesn't require additional space.
Method #7 : Using operator.countOf() method
Approach
- Check whether count of 0 in values of dictionary is equal to length of dictionary using operator.countOf() method
- If yes make res equals to True
- Display res
Python3
# Python3 code to demonstrate working of
# Check if all values are 0 in dictionary
# Initialize dictionary
test_dict = {'gfg' : 0, 'is' : 1, 'best' : 0}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Check if all values are 0 in dictionary
res = False
x=list(test_dict.values())
import operator
if(operator.countOf(x,0)==len(x)):
res=True
# printing result
print("Does all keys have 0 value ? : " + str(res))
OutputThe original dictionary is : {'gfg': 0, 'is': 1, 'best': 0}
Does all keys have 0 value ? : False
Time Complexity : O(N) N - length of values list
Auxiliary Space : O(1)
Similar Reads
Python - Check if all values are K in dictionary
While working with dictionary, we might come to a problem in which we require to ensure that all the values are K in dictionary. This kind of problem can occur while checking the status of the start or checking for a bug/action that could have occurred. Letâs discuss certain ways in which this task
9 min read
Python - Test if all Values are Same in Dictionary
Given a dictionary, test if all its values are the same. Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 8} Output : True Explanation : All element values are same, 8. Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 9} Output : False Explanation : All element values not same. Method #1: Using
7 min read
Check if Value Exists in Python Dictionary
We are given a dictionary and our task is to check whether a specific value exists in it or not. For example, if we have d = {'a': 1, 'b': 2, 'c': 3} and we want to check if the value 2 exists, the output should be True. Let's explore different ways to perform this check in Python.Naive ApproachThis
2 min read
Python - Check for Key in Dictionary Value list
Sometimes, while working with data, we might have a problem we receive a dictionary whose whole key has list of dictionaries as value. In this scenario, we might need to find if a particular key exists in that. Let's discuss certain ways in which this task can be performed. Method #1: Using any() Th
6 min read
Python | Check for None values in given dictionary
Many times, while working with dictionaries, we wish to check for a non-null dictionary, i.e check for None values in given dictionary. This finds application in Machine Learning in which we have to feed data with no none values. Let's discuss certain ways in which this task can be performed. Method
7 min read
Check if a Key Exists in a Python Dictionary
Python dictionary can not contain duplicate keys, so it is very crucial to check if a key is already present in the dictionary. If you accidentally assign a duplicate key value, the new value will overwrite the old one.To check if given Key exists in dictionary, you can use either in operator or get
4 min read
Python | Check if Non-None values are contiguous
Sometimes, while working with Python lists, we can have a problem in which we need to find if all the values that are valid (Non None). This has a lot of application in day-day programming. Let's discuss a method in which this task can be performed. Method 1: Using iterator + all() + any() Combinati
6 min read
Python | Check if key has Non-None value in dictionary
Sometimes, while working with Python dictionaries, we might come across a problem in which we need to find if a particular key of the dictionary is valid i.e it is not False or has a non-none value. This kind of problem can occur in the Machine Learning domain. Let's discuss certain ways in which th
6 min read
Python - Dictionary Values Division
Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Letâs discuss division of like key values and ways to solve it in this article. Met
5 min read
Check if Tuple Exists as Dictionary Key - Python
The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary.For example, given a dictionary d = {(3, 4): 'gfg',
3 min read