How to Check if multiple Variables are Equal in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
8 min

banner

# Table of Contents

  1. Check if multiple variables are equal in Python
  2. Check if multiple variables in a sequence are equal
  3. Check if multiple variables in sequence are equal to a value
  4. Check if Variable is NOT equal to multiple values in Python
  5. Check if a variable equals one of two values in Python
  6. Compare multiple variables to the same value in Python

# 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 if a == b == c: # ๐Ÿ‘‡๏ธ this runs print('Multiple variables are equal')

check if multiple variables are equal

The code for this article is available on GitHub

We used the equality == operator multiple times to check if multiple variables store the same 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

# Checking if multiple Variables are equal to a specific Value

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 are equal to specific value

The code for this article is available on GitHub

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

check if multiple variables in sequence are equal

The code for this article is available on GitHub

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.

# Check if all values in a Sequence are equal with a generator expression

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

check if all values in sequence are equal using generator expression

The code for this article is available on GitHub

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

This is a three-step process:

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

check if multiple variables in sequence are equal to value

The code for this article is available on GitHub

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.

# Check if Variable is NOT equal to multiple values in Python

You can use the not in operator to check if a variable is not equal to multiple values.

The not in operator will return True if the variable is not equal to any of the specified values and False otherwise.

main.py
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] if my_str not in multiple_values: # ๐Ÿ‘‡๏ธ this runs print('The variable is NOT equal to any of the specified values') else: print('The variable is equal to one or more of the specified values') print(my_str not in multiple_values) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

We used the not in operator to check if a variable is not equal to multiple values.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

The if block will run only if the variable is not equal to any of the specified values, otherwise the else block runs.

# Check if Variable is NOT equal to multiple values using all()

Alternatively, you can use the all() function.

The all() function will return True if the variable is not equal to any of the values and False otherwise.

main.py
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] if all(my_str != item for item in multiple_values): # ๐Ÿ‘‡๏ธ this runs print('The variable is NOT equal to any of the specified values') else: print('The variable is equal to one or more of the specified values') # ๐Ÿ‘‡๏ธ True print(all(my_str != item for item in multiple_values))
The code for this article is available on GitHub

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 variable is not equal to the current value and return the result.

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

main.py
my_str = 'another' multiple_values = ['bobby', 'hadz', 'com'] # ๐Ÿ‘‡๏ธ True print(all(my_str != item for item in multiple_values))

If the all() function finds a value that is equal to the variable, it will short-circuit returning False.

# Check if a variable equals one of two values in Python

You can use the in operator to check if a variable equals one of two values.

The in operator will evaluate to True if the variable is equal to one of the two values and False otherwise.

main.py
my_str = 'bobby' two_values = ('bobby', 'hadz') if my_str in two_values: # ๐Ÿ‘‡๏ธ this runs print('The variable is equal to one of the two values') else: print('The variable is NOT equal to any of the specified values') print(my_str in two_values) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

We used the in operator to check if a variable is equal to one of two values.

We grouped the values in a tuple, so we can test for membership.

The in operator tests for membership. For example, x in t evaluates to True if x is a member of t, otherwise it evaluates to False.

If the variable is equal to at least one of the values, the if block runs, otherwise, the else block runs.

main.py
my_str = 'bobby' two_values = ('bobby', 'hadz') print(my_str in two_values) # ๐Ÿ‘‰๏ธ True print('another' in two_values) # ๐Ÿ‘‰๏ธ False

# Check if a variable equals one of two values using any()

Alternatively, you can use the any() function.

main.py
my_str = 'bobby' two_values = ('bobby', 'hadz') if any(my_str == item for item in two_values): # ๐Ÿ‘‡๏ธ this runs print('The variable is equal to one of the two values') else: print('The variable is NOT equal to any of the specified values')
The code for this article is available on GitHub

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 variable is equal to the current value and return the result.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

main.py
my_str = 'bobby' two_values = ('bobby', 'hadz') # ๐Ÿ‘‡๏ธ True print(any(my_str == item for item in two_values))

If the condition is met, the any() function short-circuits and returns True.

If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

main.py
my_str = 'another' two_values = ('bobby', 'hadz') if any(my_str == item for item in two_values): print('The variable is equal to one of the two values') else: # ๐Ÿ‘‡๏ธ this runs print('The variable is NOT equal to any of the specified values') # ๐Ÿ‘‡๏ธ False print(any(my_str == item for item in two_values))

The variable is not equal to any of the specified values, so the condition is never met and the else block runs.

# Compare multiple variables to the same value in Python

Use the boolean OR operator to compare multiple variables to the same value.

main.py
a = 'dev' b = 'test' c = 'ship' # โœ… Check if one of multiple variables is equal to a value if a == 'dev' or b == 'dev' or c == 'dev': # ๐Ÿ‘‡๏ธ this runs print('One or more of the variables is equal to dev') else: print('None of the variables is equal to dev') # ------------------------------------------------------- # โœ… Check if one of multiple variables is equal to a value using `in` if 'dev' in (a, b, c): print('One or more of the variables is equal to dev') # ------------------------------------------------------- # โœ… Check if multiple variables are equal to a value if a == b == c == 'dev': print('The variables are equal to dev') # ------------------------------------------------------- # โœ… Check if multiple variables are equal to value using `and` if a == 'dev' and b == 'dev' and c == 'dev': print('The variables are equal to dev')
The code for this article is available on GitHub

We used the boolean or operator to check if one of the multiple variables is equal to a value.

The if block in the example runs if either of the 3 conditions is met.

You don't have to use the equality (==) operator, you can use any of the other comparison operators.

Alternatively, you can use the in operator.

# Compare multiple variables to the same value using in operator

This is a three-step process:

  1. Group the variables in a tuple.
  2. Use the in operator to check if the value is contained in the tuple.
  3. The in operator will return True if the value is equal to at least one of the variables.
main.py
a = 'dev' b = 'test' c = 'ship' # โœ… Check if one of multiple variables is equal to a value using `in` if 'dev' in (a, b, c): print('One or more of the variables is equal to dev')
The code for this article is available on GitHub

The in operator tests for membership. For example, x in t evaluates to True if x is a member of t, otherwise it evaluates to False.

If you need to compare multiple variables to the same value where all conditions have to be met, use the and operator.

main.py
a = 'dev' b = 'test' c = 'ship' if a == 'dev' and b == 'dev' and c == 'dev': print('The variables are equal to dev')

The if block runs only if all of the variables are equal to the specified value.

You can use the same approach with different comparison operators.

Here is an example that checks if multiple variables are all greater than 0.

main.py
a = 1 b = 2 c = 3 if a > 0 and b > 0 and c > 0: # ๐Ÿ‘‡๏ธ this runs print('The variables are all greater than 0')

If you need to check if multiple variables are equal to a value, use the equality (==) operator multiple times.

main.py
a = 'dev' b = 'dev' c = 'dev' if a == b == c == 'dev': # ๐Ÿ‘‡๏ธ this runs print('The variables are equal to dev')
The code for this article is available on GitHub

The if block runs only if all of the variables store the specified value.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev