iceburger
iceburger

Reputation: 99

How to get a list of values between two values without knowing which is greater in Python?

I need to create a list of the values between x and y, not including x and y. I'm aware range() can be used to accomplish this. However, I need to be able to do this without knowing whether x or y is greater.

The main problems I'm running into are that range() does not seem to work when the first argument is greater, and I can't know whether to increment x or y in order to make sure x and y are not included.

I'm aware there's a way to do this in several lines with if statements and for loops, however I'm looking for a method that only takes one or two lines, perhaps with a built-in function.

Upvotes: 0

Views: 41

Answers (2)

Metete-Welete
Metete-Welete

Reputation: 21

You can try using the min max function

for i in range(min(x,y)+1, max(x,y)):
    print(i)

Upvotes: 2

Qudus
Qudus

Reputation: 1520

for num in range(min(x, y), max(x, y)) 
   ... do something... 

Upvotes: 1

Related Questions