Check if multiple variables are equal in Python

avatar

Borislav Hadzhiev

Last updated: Jul 15, 2022

banner

Photo from Unsplash

Check if multiple variables are equal in Python #

Use the equality operator to check if multiple variables are equal, e.g. if a == b == c:. The expression will compare the variables on the left-hand and right-hand sides of the equal signs and will return True if they are equal.

main.py
a = 123 b = 123 c = 123 # ✅ Check if multiple variables are equal to value if a == b == c == 123: # 👇️ this runs print('Variables are equal to value') # ------------------------------------------------- # ✅ Check if multiple variables are equal if a == b == c: # 👇️ this runs print('Multiple variables are equal') # ------------------------------------------------- my_list = [a, b, c] # ✅ Check if multiple variables in a sequence (e.g. list) are equal to value if all(item == 123 for item in my_list): # 👇️ this runs print('Variables are equal to value') # ------------------------------------------------- # ✅ Check if multiple variables in a sequence (e.g. a list) are equal if my_list.count(my_list[0]) == len(my_list): # 👇️ this runs print('Multiple variables are equal')

We used the equality == operator multiple times to check if multiple variables store the same value.

main.py
a = 123 b = 123 c = 123 if a == b == c: # 👇️ this runs print('Multiple variables are equal') # ------------------------------------------------- if a == b == c == 123: # 👇️ this runs print('Variables are equal to value')

If the variables store the same value, the expression returns True, otherwise False is returned.

main.py
a = 123 b = 123 c = 123 are_equal = a == b == c print(are_equal) # 👉️ True

If you need to check if multiple variables are equal to a specific value, add another equal sign to the expression and compare to the value.

main.py
a = 123 b = 123 c = 123 are_equal_to_value = a == b == c == 123 print(are_equal_to_value) # 👉️ True if a == b == c == 123: # 👇️ this runs print('Variables are equal to value')

Check if multiple variables in a sequence are equal #

To check if multiple variables in a sequence (e.g. list, tuple) are equal:

  1. Use the count() method to count the occurrences of the first item in the sequence.
  2. Compare the count with the length of the sequence.
  3. If the two values are equal, all values in the sequence are equal.
main.py
a = 123 b = 123 c = 123 my_list = [a, b, c] if my_list.count(my_list[0]) == len(my_list): # 👇️ this runs print('Multiple variables are equal')

The list.count() method takes a value as an argument and returns the number of times the provided value appears in the list.

If the number of times the first item appears in the sequence is the same as the length of the sequence, then all values in the sequence are equal.

Alternatively, you can use a generator expression to check if all values in a sequence are equal.

main.py
a = 123 b = 123 c = 123 my_list = [a, b, c] if all(item == my_list[0] for item in my_list): # 👇️ this runs print('Multiple variables are equal')

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we check if the current value is equal to the first list element and return the result.

The all() built-in function takes an iterable as an argument and returns True if all elements of the iterable are truthy (or the iterable is empty).

Check if multiple variables in sequence are equal to a value #

To check if multiple variables in a sequence are equal to a value:

  1. Use a generator expression to iterate over the sequence.
  2. On each iteration, compare the current item to the specific value.
  3. Pass the generator object to the all() function.
main.py
a = 123 b = 123 c = 123 my_list = [a, b, c] if all(item == 123 for item in my_list): # 👇️ this runs print('Variables are equal to value')

On each iteration, we compare the current list item to a specific value and return the result.

If all of the comparisons evaluate to True, the all() function will also return True.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.