# How to Check if multiple Variables are Equal in Python

Last updated: Jan 27, 2023
8 min

## #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
```Copied!```a = 123
b = 123
c = 123

if a == b == c:
# ๐๏ธ this runs
print('Multiple variables are equal')
``````

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
```Copied!```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
```Copied!```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
```Copied!```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.

## #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
```Copied!```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

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
```Copied!```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`.

## #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
```Copied!```my_str = 'another'

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
``````

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
```Copied!```my_str = 'another'

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

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
```Copied!```my_str = 'another'

# ๐๏ธ 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
```Copied!```my_str = 'bobby'

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
``````

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
```Copied!```my_str = 'bobby'

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
```Copied!```my_str = 'bobby'

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

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
```Copied!```my_str = 'bobby'

# ๐๏ธ 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
```Copied!```my_str = 'another'

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
```Copied!```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')
``````

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
```Copied!```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 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
```Copied!```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
```Copied!```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
```Copied!```a = 'dev'
b = 'dev'
c = 'dev'

if a == b == c == 'dev':
# ๐๏ธ this runs
print('The variables are equal to dev')
``````

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