0

I'm trying to figure out if there's a shorter way of writing this if statement (wrote a very basic, unrealistic if statement just so it's easy to see the point.

 x = 3 if x == 1 or x == 2 or x == 3 or x==4: print x else: print "nope" 

I would like to write something along the lines of:

if x == or(1,2,3,4): 

is there any way of doing that? Or do I have to write out every option?

Thanks, Sean

1
  • format your code properly Commented Oct 15, 2013 at 19:24

3 Answers 3

7

you want:

if x in (1, 2, 3, 4): 
Sign up to request clarification or add additional context in comments.

16 Comments

I really miss this in other languages.
If you have more than a handful of values, if x in {1, 2, 3, 4}: (using a set) is better. But for just four of them, it won't make a difference.
And in that case, if you're using a version of Python without set literals, define the set before the if statement.
@kindall: Well, set((1,2,3,4)) isn't that terrible, certainly not the most terrible thing you'll face in Python 2.3…
In Javascript you could do [1, 2, 3, 4].indexOf(x) + 1 to avoid the ugly != -1 although that is arguably even worse. :-)
|
3

Use the in operator:

if x in range(1, 5) 

On Python2 you can use xrange.

On Python 3.2+ it is recommended to use set literals:

if x in {1, 2, 3, 4}:

From docs:

Python’s peephole optimizer now recognizes patterns such xin {1, 2, 3} as being a test for membership in a set of constants. The optimizer recasts the set as a frozenset and stores the pre-built constant. Now that the speed penalty is gone, it is practical to start writing membership tests using set-notation. This style is both semantically clear and operationally fast.

13 Comments

This is a good solution for Python 3, but the OP is using Python 2, which means it will have to create a list and then search it linearly. Not a horrible problem for only 4 values, but for only 4 values I don't think it's more concise or readable than just writing [1, 2 ,3, 4].
For Python 2, just use xrange instead.
@chepner: I don't think it's documented anywhere that in on an xrange is constant time in 2.x.
In fact, it isn't constant time in 2.7; try %timeit 10000 in xrange(20000), %timeit 100000 in xrange(200000), %timeit 1000000 in xrange(2000000) and it's clearly linear.
Anyway, for a range with a default step and integer values, you can just write 1 <= x < 5
|
0
if x>=1 or x<=4: 

will do the job

4 Comments

Python allows the even shorter if 1 <= x <= 4, which also implies and (as DSM points out).
What if x==10? (IOW, I think you mean and.) But this would also let x==1.5 pass too.
I'd actually prefer if 0 < x < 5 for that type of comparison.
the (1,2,3,4) was just made up numbers; pretend like it's (1, 17, 3, 10000,4) or something. Sorry for being unclear about that, but the "if x in (1,17,3,10000,4)" is the ideal method I was looking for. Thanks, though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.