How to Compare Multiple Variables in Python
This guide explores various efficient and Pythonic methods for comparing multiple variables in Python. We'll cover:
- Checking if multiple variables have the same value.
- Checking if multiple variables are equal to a specific value.
- Checking if a variable is not equal to multiple values.
- Checking if a variable is equal to any of several values.
- Using the
all()
andany()
functions for cleaner comparisons.
Checking if All Variables Have the Same Value
Using multiple equality comparisons (==
)
The simplest way to check if all variables have the same value is to chain equality comparisons:
a = 123
b = 123
c = 123
if a == b == c:
print('Multiple variables are equal')
else:
print('Multiple variables are not equal')
Output:
Multiple variables are equal
- This approach is readable and efficient for a small number of variables.
Checking equality within a sequence (list/tuple)
If your variables are already in a list or tuple (or you can easily put them in one), you can use the count()
method and check the length, or use the all()
function:
a = 123
b = 123
c = 123
my_list = [a, b, c]
#Method 1: count()
if my_list.count(my_list[0]) == len(my_list):
print('Multiple variables are equal') # Output: Multiple variables are equal
else:
print('Multiple variables are not equal')
#Method 2: all()
if all(item == my_list[0] for item in my_list):
print('Multiple variables are equal') # Output: Multiple variables are equal
count()
returns the number of occurrences of a value in a list.all()
is used with a generator expression that checks if all the elements of the list are equal to the first element of the list.- Both methods are very efficient for checking equality within a sequence.
Checking if All Variables Equal a Specific Value
To check if all variables are equal to a specific value, you can again use chained equality or all()
:
a = 123
b = 123
c = 123
# Chained equality
if a == b == c == 123:
print('Variables are equal to the value') # Output: Variables are equal to the value
# Using all() with a list/tuple
variables = [a, b, c]
if all(v == 123 for v in variables):
print('Variables are equal to the value') # Output: Variables are equal to the value
Checking if a Variable is NOT Equal to Multiple Values
Using not in
The most readable and Pythonic way to check if a variable is not equal to any of several values is to use the not in
operator:
my_str = 'another'
multiple_values = ['tutorial', 'reference', 'com']
if my_str not in multiple_values:
print('The variable is NOT equal to any of the specified values')
else:
print('The variable is equal to one of the specified values')
print(my_str not in multiple_values) # Output: True
Output:
The variable is NOT equal to any of the specified values
True
- The
not in
method is much more readable than multiple chained!=
conditions.
Using all()
You can also use all()
with a generator expression, which is functionally equivalent to not in
but might be preferred in some contexts for consistency:
my_str = 'another'
multiple_values = ['tutorial', 'reference', 'com']
if all(my_str != item for item in multiple_values):
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')
Output:
The variable is NOT equal to any of the specified values
Checking if a Variable Equals ANY of Several Values
Using in
(Recommended)
To check if a variable is equal to any of several values, use the in
operator:
my_str = 'tutorial'
two_values = ('tutorial', 'reference') # Tuple is slightly more efficient than a list
if my_str in two_values:
print('The variable is equal to one of the two values') # Output: The variable is equal...
else:
print('The variable is NOT equal to any of the specified values')
my_str in two_values
checks if the string is equal to any of the values, and returnsTrue
if it is.
Using any()
For completeness, here's the any()
approach, which is equivalent but less readable than in
:
my_str = 'tutorial'
two_values = ('tutorial', 'reference')
if any(my_str == item for item in two_values):
print('The variable is equal to one of the two values') # Output: The variable is equal...
else:
print('The variable is NOT equal to any of the specified values')
Conclusion
This guide presented different ways to compare variables and values in Python.
You learned how to:
- compare the same value to a list, using
all()
or thein
operator. - Use chained equality checks (
a == b == c
) when you need to verify that all variables hold the same value. - Use
in
andnot in
for concise membership testing. For checking multiple variables against a single value or another set of variables,all()
with a generator expression is generally the most efficient and readable approach.
Choose the technique that best reflects the logic of your comparison and enhances code clarity.