How to Check if All Values in a Python Dictionary are Equal
This guide demonstrates various methods for checking if all values in a Python dictionary are equal. We'll explore using the all()
function with a generator expression (the most concise and Pythonic way), using set operations, using list.count()
and using a for
loop, explaining the trade-offs of each.
Using all()
with a Generator Expression (Recommended)
The most Pythonic and efficient way to check if all dictionary values are equal is to use the all()
function combined with a generator expression:
my_dict = {
'tutorial': 5,
'reference': 5,
'com': 5
}
first_value = list(my_dict.values())[0] # Get first value.
all_equal = all(value == first_value for value in my_dict.values())
print(all_equal) # Output: True
my_dict = {
'tutorial': 5,
'reference': 6, # Not the same value
'com': 5
}
first_value = list(my_dict.values())[0]
all_equal = all(value == first_value for value in my_dict.values())
print(all_equal) # Output: False
first_value = list(my_dict.values())[0]
: This line gets the first value in the dictionary. We'll compare all other values to this one. We convert thedict_values
view object to a list to allow indexing.all(value == first_value for value in my_dict.values())
: This is the core logic.my_dict.values()
: Gets a "view" of all the values in the dictionary. This is efficient; it doesn't create a new list.value == first_value for value in my_dict.values()
: This is a generator expression. It efficiently iterates through the dictionary's values and, for eachvalue
, checks if it's equal tofirst_value
. It yields a sequence ofTrue
orFalse
values.all(...)
: Theall()
function returnsTrue
if all values produced by the generator expression areTrue
. It short-circuits: as soon as it finds aFalse
value, it stops and returnsFalse
. This makes it efficient.
This method is concise, readable, and efficient because it avoids creating intermediate lists or sets unless necessary.
Using Set Operations (Efficient for Unique Values)
If you only care about whether all values are equal (and don't need to know what that value is), you can convert the dictionary's values to a set
. If all values are equal, the set will have a length of 1:
my_dict = {
'tutorial': 5,
'reference': 5,
'com': 5
}
all_equal = len(set(my_dict.values())) == 1
print(all_equal) # Output: True
my_dict = {
'tutorial': 5,
'reference': 6,
'com': 5
}
all_equal = len(set(my_dict.values())) == 1
print(all_equal) # Output: False
set(my_dict.values())
: Creates a set from the dictionary's values. Sets automatically remove duplicates.len(...) == 1
: Checks if the resulting set has only one element. If it does, all original values were identical.
This method is very efficient, especially if the dictionary has many values, but it doesn't tell you what the common value is. It only tells you if they are all equal.
Using list.count()
You can use list.count()
, but this is generally less efficient than the all()
or set-based approaches:
my_dict = {
'tutorial': 5,
'reference': 5,
'com': 5
}
first_value = list(my_dict.values())[0]
all_equal = list(my_dict.values()).count(first_value) == len(my_dict)
print(all_equal) # Output: True
my_dict = {
'tutorial': 5,
'reference': 6,
'com': 5
}
first_value = list(my_dict.values())[0]
all_equal = list(my_dict.values()).count(first_value) == len(my_dict)
print(all_equal) # Output: False
- This method requires creating a list of all values and then counting the occurrences of the first value. It is inefficient for larger dictionaries.
Using a for
Loop (Most Explicit)
For completeness, here's how you'd do it with a for
loop:
my_dict = {
'tutorial': 5,
'reference': 5,
'com': 5
}
all_values_equal = True
first_value = list(my_dict.values())[0]
for value in my_dict.values():
if value != first_value:
all_values_equal = False
break # Exit loop as soon as a difference is found
print(all_values_equal) # Output: True
my_dict = {
'tutorial': 5,
'reference': 6,
'com': 5
}
all_values_equal = True
first_value = list(my_dict.values())[0]
for value in my_dict.values():
if value != first_value:
all_values_equal = False
break
print(all_values_equal) # Output: False
- The code iterates through the values of the dictionary and sets the
all_values_equal
variable to False as soon as it finds a different value. break
statement breaks out of the loop as soon as a difference is found.