Unless you need to microoptimise, I'd just to this
def myFunc(x, y):
for i in range(*sorted((x, y))):
...
This is faster though
def myFunc(x, y):
for i in range(x,y) if x<y else range(y,x):
...
minmax.py
def f1(x, y):
for i in range(min(x, y), max(x, y)):
pass
def f2(x, y):
for i in range(*sorted((x, y))):
pass
def f3(x, y):
for i in range(x, y) if x<y else range(y, x):
pass
def f4(x, y):
if x>y:
x,y = y,x
for i in range(x, y):
pass
def f5(x, y):
mn,mx = ((x, y), (y, x))[x>y]
for i in range(x,y):
pass
benchmarks (f3 is fastest regardless of the order)
$ python -m timeit -s"import minmax as mm" "mm.f1(1,2)"
1000000 loops, best of 3: 1.93 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f2(1,2)"
100000 loops, best of 3: 2.4 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f3(1,2)"
1000000 loops, best of 3: 1.16 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f4(1,2)"
100000 loops, best of 3: 1.2 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f5(1,2)"
1000000 loops, best of 3: 1.58 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f1(2,1)"
100000 loops, best of 3: 1.88 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f2(2,1)"
100000 loops, best of 3: 2.39 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f3(2,1)"
1000000 loops, best of 3: 1.18 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f4(2,1)"
1000000 loops, best of 3: 1.25 usec per loop
$ python -m timeit -s"import minmax as mm" "mm.f5(2,1)"
1000000 loops, best of 3: 1.44 usec per loop