Test multiple variables against a single value in Python

avatar

Borislav Hadzhiev

Last updated: Jul 15, 2022

banner

Check out my new book

Test multiple variables against a single value in Python #

To test multiple variables against a single value:

  1. Wrap the variables in a set object, e.g. {a, b, c}.
  2. Use the in operator to test if the value is stored in any of the variables.
  3. The in operator will return True if the value is stored in at least one of the variables.
main.py
# ✅ test multiple variables against single value using tuple if 'a' in (a, b, c): print('value is stored in at least one of the variables') # --------------------------------------------------------- # ✅ test multiple variables against single value using tuple if 'a' in {a, b, c}: print('value is stored in at least one of the variables') # --------------------------------------------------------- # ✅ test multiple variables against single value (OR operator chaining) if a == 'a' or b == 'a' or c == 'a': print('value is stored in at least one of the variables')
The best way to test multiple variables for equality against a single value is to wrap the variables in a sequence (e.g. a set, a tuple, or a list) and use the in operator.

Using a set object should be slightly faster than the other sequences as set objects are optimized for membership testing.

main.py
a = 'a' b = 'b' c = 'c' if 'a' in {a, b, c}: print('value is stored in at least one of the variables')
Set objects are an unordered collection of unique elements.

Alternatively, you could wrap the variables in a tuple.

main.py
a = 'a' b = 'b' c = 'c' if 'a' in (a, b, c): print('value is stored in at least one of the variables')
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

An alternative approach is or operator chaining.

Use the or operator to test multiple variables against a single value, e.g. if a == 'a' or b == 'a' or c == 'a':. The or operator will return True if the value is stored in at least one of the variables.

main.py
a = 'a' b = 'b' c = 'c' if a == 'a' or b == 'a' or c == 'a': print('value is stored in at least one of the variables')

We used the or operator to separate each expression.

The if statement first checks if the a variable stores a value of "a", and if it does, the if statement short-circuits returning True without checking any of the following conditions.

If the condition evaluates to False, the next condition is evaluated until one returns a truthy value or all conditions are exhausted.

If the if block runs, the value is stored in at least one of the variables.

The expression x or y returns the value to the left if it's truthy, otherwise the value to the right is returned.

main.py
# 👇️ default value print(False or 'default value') # 👇️ hello print('hello' or 'default value')
The boolean or operator will return the value to the right if the value to the left is falsy.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

So if the value to the left is any of the aforementioned falsy values, the value to the right is returned.

main.py
print(False or 'default value') # 👉️ default value print(0 or 'default value') # 👉️ default value print("" or 'default value') # 👉️ default value

Using the or operator to test multiple values against a single value can get repetitive.

If you have more than two variables to check against, wrap the variables in a set object and use the in operator instead.

main.py
a = 'a' b = 'b' c = 'c' if 'a' in {a, b, c}: print('value is stored in at least one of the variables')
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.