I have a function that is passed two values and then iterates over the range of those values. The values can be passed in any order, so I need to find which one is the lowest first. I had the function written like this:
def myFunc(x, y):
if x > y:
min_val, max_val = y, x
else:
min_val, max_val = x, y
for i in range(min_val, max_val):
...
But to save some screen space, I ended up changing it to:
def myFunc(x, y):
min_val, max_val = sorted([x, y])
for i in range(min_val, max_val):
...
How bad is this? Is there a better way that’s still one line?
Unless you need to microoptimise, I’d just to this
This is faster though
minmax.py
benchmarks (f3 is fastest regardless of the order)