How to Test Multiple Variables Against a Value in Python?

To test multiple variables x, y, z against a value in Python, use the expression value in {x, y, z}. Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value.

Here’s the code:

Exercise: Add a fourth variable and run the modified code!

In this article, you’ll learn about an interesting problem in programming. Interesting because a quick glance could give us a feeling of this problem being simple. However, diving into it can help us learn some fundamentals about Boolean operators and some data types in a way we might not have thought about earlier. In fact, you could find yourself in this situation if you’re a beginner. We will try and explore some solutions to this problem using Python taking into consideration three approaches.

  • The Incorrect Approach
  • The Correct Approach
  • The Best Approach

Let’s see some code walkthrough to understand it.

Doing It The Wrong Way — The Incorrect Approach

Let’s consider the above lines of code. We have three variables, x = 0, y = 1 and z = 3 and want to print a list, “list1” as output which checks at line 9, 11, 13 and 15 respectively, if either of x, y or z equals to 0, 1, 2, or 3 and append to our output list – list1, the values, c, d, e, and f based on the output condition.

Pause for a moment, and observe. What should be the output for the above lines of code? Now run the above code snippet. If you were expecting the output – [‘c’, ‘d’, ‘f’], read along carefully.

Let’s find out what is happening with our code. To understand it clearly, we must appreciate a few things first. The logical operators in Python do not work in a way you would expect them to work in English. For instance, let us look at line 9 of the code snippet.

From Python documentation (Boolean operations), the expression “x or y” first evaluates x. If x is true, its value is returned. Otherwise, y is evaluated, and the resulting value is returned.

The second point to note here is about operator precedence. The “or” operator has lower precedence than “==” operator, and hence equality is evaluated first.

The line number 9 returns “1” (True) as output. So line number 9 and 10 simplifies to:

Similarly, line 11, 13, and 15 each return 1 as output and hence (therefore) “d”, “e” and “f” gets appended to the list. Below code snippet shows the output for all the conditions individually:

Operations and built-in functions that have a Boolean result always return 0 or False for a false and 1 or True for true, unless otherwise stated. Thus our final output to code snippet in fig1 returns [“c”, “d”, “e”, “f”]

Doing It The Right Way — The Correct Approach

Now we build upon our mistakes above and look at a solution that should give us the expected output. Let’s modify the above code snippet a little and see how it changes the expected output:

The difference is evident here as we use the statement “x == 0 or y == 0 or z == 0” instead of “x or y or z == 0”. In this case, each variable here i.e. x, y, z are checked for equivalence one after the other and if the condition satisfies, the corresponding alphabet i.e c, d, e, f is appended to our list 1 respectively. Run the above code and check the output.

Doing It The Better Way: A Better Approach

With Python, we get myriads of collection/sequence data types and some in-built keywords that we can work with. The original idea of different collection/sequence data types and the keywords available for our use is to simplify how we can create a collection/sequence of data and access them based on the problem statement we are dealing with. We will check two data structures – Tuples and Sets, which bring in a good use case for the scenario that we are dealing with.

The “in” keyword can be used along with sequences like lists, sets, and tuples. It returns True or False based on whether the operand on the left is found or not in the sequence. Let’s check out the code snippet below:

Using Tuples to Do it Better:

In this case, we define variables x, y, and z and then check for the specific value membership with tuples in the above case. Hence, on line 9 we check, 0 to be in the tuple (0, 1, 3), which is a membership check, and if the condition is True, we append alphabet “c” to our list1. Then the membership check is done for values 1, 2, and 3, respectively, and based on whether the condition is True or False, the respective alphabet values of “c”, “d”, “e”, “f” are appended to our list1.

To clarify further, on line 9 we get the output as True and hence “c” is appended to list1 whereas, on line 13, where we check for membership of 2 in the tuple (0, 1, 3), the condition is False and hence “e” is not appended to list1.

Using Sets to Do it Better:

Here’s the code for easier copy&pasting:

x = 0
y = 1
z = 3

list1 = []

if 0 in {x, y, z}:
if 0 in {x, y, z}:
if 0 in {x, y, z}:
if 0 in {x, y, z}:

# ['c', 'd', 'e', 'f']

This solution is the same as the above solution with Tuples. The only difference is the data structure that we use here. With this solution we perform membership checks using Sets and based on the condition being True or False, appending the respective alphabet “c”, “d”, “e”, “f” to list1.

I hope this explanation helps. Try and get your hands dirty running the above code snippets, and you will have a better understanding by the time you are done.


This article is contributed by Finxter Abhigyan Ojha. You can find his Upwork profile here.