Check if a variable equals one of two values in Python

avatar

Borislav Hadzhiev

Last updated: Aug 12, 2022

banner

Photo from Unsplash

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

Use the in operator to check if a variable equals one of two values, e.g. if my_str in ('first', 'second'):. 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 = 'apple' two_values = ('kiwi', 'apple') 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
my_str = 'apple' two_values = ('kiwi', 'apple') print(my_str in two_values) # 👉️ True print('hello' in two_values) # 👉️ False

Alternatively, you can use the any() function.

Use the any() function to check if a variable is equal to one of two values, e.g. if any(my_str == item for item in ('first', 'second')):. The any() function will return True if the variable is equal to one of the two values and False otherwise.

main.py
my_str = 'apple' two_values = ('kiwi', 'apple') 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
my_str = 'apple' two_values = ('kiwi', 'apple') # 👇️ 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 = 'apple' two_values = ('kiwi', 'banana') 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.

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.