Last updated: Apr 10, 2024
Reading timeยท5 min
set
list.count()
for
loopTo check if all values in a dictionary are equal:
dict.values()
method to get the first value of the dictionary.all()
function to iterate over the dictionary's values.a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } # โ Check if all values in dict are equal to a specific value all_equal_to_5 = all(value == 5 for value in a_dict.values()) print(all_equal_to_5) # ๐๏ธ True # ------------------------------------------------- # โ Check if all values in dict are equal first_value = list(a_dict.values())[0] print(first_value) # ๐๏ธ 5 all_equal = all(value == first_value for value in a_dict.values()) print(all_equal) # ๐๏ธ True
The first example checks if all of the values in a dictionary are equal to a specific value.
The second example checks if all of the values in a dictionary are equal.
We used the dict.values()
method to get a view of the dictionary's values.
a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } # ๐๏ธ dict_values([5, 5, 5]) print(a_dict.values())
The all() built-in function
takes an iterable as an argument and returns True
if all elements in the
iterable are truthy (or the iterable is empty).
We passed a
generator expression
to the all()
function.
On each iteration, we compare the current dictionary value to the number 5
and
return the result.
a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } all_equal_to_5 = all(value == 5 for value in a_dict.values()) print(all_equal_to_5) # ๐๏ธ True if all_equal_to_5: # ๐๏ธ this runs print('All values in the dictionary are equal to the specified value') else: print('Not all values in the dictionary are equal to the specified value')
all()
function returns True
only if the condition is met for all values in the dictionary.If the condition isn't met at least once, the all()
function short-circuits
and returns False
.
You can use the same approach if you simply need to check if all of the values in a dictionary are equal.
a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } first_value = list(a_dict.values())[0] print(first_value) # ๐๏ธ 5 all_equal = all(value == first_value for value in a_dict.values()) print(all_equal) # ๐๏ธ True
We used the list()
class to convert the view of the dictionary's values to a
list to be able to access the first value.
The last step is to compare the first value to all other values in the dictionary.
Alternatively, you can use a set
object.
set
This is a three-step process:
set()
class to convert the dictionary's values to a set
object.len()
function to get the length of the set
.set
has a length of 1
, then all values in the dictionary are
equal.a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } all_equal = len(set(a_dict.values())) == 1 print(all_equal) # ๐๏ธ True
The set() class takes an
iterable optional argument and returns a new set
object with elements taken
from the iterable.
set
all duplicates are automatically removed.The last step is to use the len()
function to get the length of the set
object and compare the result to 1
.
If the set
has a length of 1, then all values in the dictionary are equal.
Alternatively, you can use the list.count()
method.
list.count()
This is a three-step process:
dict.values()
method to get the first value in the dictionary.list.count()
method to check if the occurrences of the first value
are equal to the dictionary's length.a_dict = { 'bobby': 5, 'hadz': 5, 'com': 5 } first_value = list(a_dict.values())[0] all_equal = list(a_dict.values()).count(first_value) == len(a_dict) print(all_equal) # ๐๏ธ True
list()
class to convert the dictionary's values to a list to be able to use the list.count()
method.The list.count()
method takes a value and returns the number of times the
provided value appears in the list.
my_list = [None, None, None] print(my_list.count(None)) # ๐๏ธ 3 print(my_list.count('hello')) # ๐๏ธ 0
If the number of occurrences of the first value in the dictionary is equal to the dictionary's length, then all values in the dictionary are equal.
for
loopThis is a four-step process:
dict.values()
method to get a view of the dictionary's values.for
loop to iterate over the view object.a_dict = { 'bobby': 0, 'hadz': 0, 'com': 0 } all_values_equal = True first_value = list(a_dict.values())[0] for value in a_dict.values(): if value != first_value: all_values_equal = False break print(all_values_equal) # ๐๏ธ True
We initialized the all_values_equal
variable to True
.
On each iteration of the for loop, we check if the current value is not equal to the first value.
all_values_equal
variable to False
and break out of the loop.The break statement breaks out of the
innermost enclosing for
or while
loop.
If all of the values in the dictionary are equal, the condition in the if
statement is never met and the all_values_equal
variable remains set to
True
.
Which approach you pick is a matter of personal preference. I'd use the all()
function because I find it quite intuitive and direct.
You can learn more about the related topics by checking out the following tutorials: