Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all values of a dictionary are 0

I want to check if all the values, i.e values corresponding to all keys in a dictionary are 0. Is there any way to do it without loops? If so how?

like image 568
vishu rathore Avatar asked Feb 07 '16 13:02

vishu rathore


People also ask

How do you check if all values are equal in a dictionary?

Method #2 : Using set() + values() + len() This is yet another way in which this task can be performed. In this, we extract all the values using values() and set() is used to remove duplicates. If length of the extracted set is 1, then all the values are assumed to be similar.

How do you check if a value in a dictionary is empty?

# Checking if a dictionary is empty by using the any() function empty_dict = {} if any(empty_dict): print('This dictionary is not empty! ') else: print('This dictionary is empty! ') # Returns: This dictionary is empty!

How do you check if a value exists in a dictionary Python?

Check if a value exists in a dictionary: in operator, values() To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use not in to check if a value does not exist in a dictionary.


1 Answers

use all():

all(value == 0 for value in your_dict.values()) 

all returns True if all elements of the given iterable are true.

like image 112
Shawn Avatar answered Oct 05 '22 22:10

Shawn