Python Tricks: Check Multiple Variables against Single Value | by Louis Chan | Medium | Towards Data Science

Welcome to a series of short posts each with handy Python tricks that can help you become a better Python programmer. In this blog, we will look into variable comparisons.
You have variables x, y, z, and you have also got a constant c that you would like to check whether it exists in any of the three variables. We can do it with a brute-force approach with the following logic, but are there better solutions?
if (x == c) or (y == c) or (z == c):    print("It exists!")else:    print("It does not exist!")
if any(i == c for i in (x, y, z)):    print("It exists!")else:    print("It does not exist!")
If in case you would like to check whether all of x, y, z are of value c, you can also change any to all. The two functions check whether any/all of the provided iterator is evaluated as True. That means if we have a tuple of t = (0, 1, 2), all(t) will return False as the first element 0 will be evaluated as False. This also opens up a lot of flexibility as common values like 0, None, [], (), "", {} would all be evaluated as False.
Note that x, y, z have been combined to a tuple rather than a list for marginally better memory performance. If you would like to read more about the memory usage of data structures, I have previously written a piece on it:
towardsdatascience.com
Instead of creating a tuple and iterate it through, we can also just do it with a membership test and skip the need of any.
# Membership test with listif c in [x, y, z]:    print("It exists!")else:    print("It does not exist!")# Membership test with tupleif c in (x, y, z):    print("It exists!")else:    print("It does not exist!")# Membership test with setif c in {x, y, z}:    print("It exists!")else:    print("It does not exist!")
Although both list, tuple, and set supports the membership test as they are all iterables in Python, there are minor differences when choosing which one to use. For example, using tuple will take up the least memory among the three assuming that the uniqueness of values within the tuple is high. On the other hand, implementation of set allows constant-cost membership test meaning that it has the least computational complexity among the three though constructing a set might outweigh the benefits of it. While for list, there just isn’t any edge of using variable-length arrays in this context so forget about it.
That’s about it for this blog post! I hope you have found this useful. If you are interested in other Python tricks, I have put together a list of these short blogs for you:
Python Tricks: Flattening Lists
Python Tricks: Simplifying If Statements & Boolean Evaluation
Python Tricks: How to Check Table Merging with Pandas
If you want to learn more about Python, Data Science, or Machine Learning, you may want to check out these posts:
7 Easy Ways for Improving Your Data Science Workflow
Efficient Conditional Logic on Pandas DataFrames
Memory Efficiency of Common Python Data Structures
Parallelism with Python
Essential Jupyter Extension for Data Science Set Up
Efficient Root Searching Algorithms in Python
If you would like to learn more about how to apply machine learning to trading & investing, here are some other posts that may be of interest:
Genetic Algorithm for Trading Strategy Optimization in Python
Genetic Algorithm — Stop Overfitting Trading Strategies
ANN Recommendation System for Stock Selection
www.linkedin.com
Special thanks to Sander Koelstra for pointing out some misleading statements.