Is there an efficient way (nice syntax) to check if a value is between two values contained in a tuple?
The best thing I could come up with is:
t = (1, 2) val = 1.5 if t <= val <= t: # do something
Is there a nicer way to write the conditional?
No, there is no dedicated syntax, using chained comparisons is the right way to do this already.
The one ‘nicety’ I can offer is to use tuple unpacking first, but that’s just readability icing here:
low, high = t if low <= val <= high:
If you are using a tuple subclass produced by
collection.namedtuple() you could of course also use the attributes here:
from collections import namedtuple range_tuple = namedtuple('range_tuple', 'low high') t = range_tuple(1, 2) if t.low <= val <= t.high: