There can be cases when we want to check whether a value is equal to one of the multiple other values.
One way that someone may start using is using or
and adding multiple checks. For example, let us say that we want to check whether a number is 11, 55, or 77. Your immediate response may be to do the following:
m = 1
if m == 11 or m == 55 or m == 77:
print("m is equal to 11, 55, or 77")
There is fortunately another quick way to do that.
All you have to do is put those values in a list and check whether this element exists inside that list:
m = 1
numbers = [11, 55, 77]
if m in numbers:
print("m is equal to 11, 55, or 77")
We can also use a set to save those numbers and do the check since sets can access each element in constant time:
m = 1
numbers = {11, 55, 77}
if m in numbers:
print("m is equal to 11, 55, or 77")
That’s pretty much it. I hope you find this useful.