Open In App

Python | Check if all values are 0 in dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

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


Output

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


Output

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


Output

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


Output

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


Output

The 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


Output

The 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

  1. Check whether count of 0 in values of dictionary is equal to length of dictionary using operator.countOf() method
  2. If yes make res equals to True
  3. 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))


Output

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



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