11

I'm trying to condense an if-elif-else statement into one line. I tried:

a == 1 ? print "one" : a == 2 ? print "two" : print "none" 

But I got a syntax-error. I have also tried:

print "one" if a == 1 else print "two" if a == 2 else print "none" 

But I also got a syntax-error.

What can I do to make any of these answers better or create a working answer?

6
  • 2
    Question: why do you want to do this? Is it for the sake of creating the one-liner, or do you feel that you'll get more clarity out of it? Commented Jan 2, 2014 at 17:51
  • @Makoto I want to do this because I don't want to write 6 lines of code. One long line of code would be better. Commented Jan 2, 2014 at 17:55
  • 2
    Don't confuse clarity and brevity - often shorter is more readable, but if you are honestly telling me that the line of code you are attempting here is clearer than the longer alternative, you have a very different definition of clear. Commented Jan 2, 2014 at 18:00
  • It's also worth noting that there isn't a 'one line if-elif-else' statement in Python. There is the ternary operator, which uses the same keywords and similar syntax, but is a fundamentally different operation with restrictions (primarily that it only supports expressions). Commented Jan 2, 2014 at 18:01
  • 1
    @Makoto That has nothing to do with what I'm trying to do. That question wants an if-else statement but I want an if-else-elif statement. Commented Jan 2, 2014 at 18:32

7 Answers 7

17

Try:

print {1: 'one', 2: 'two'}.get(a, 'none') 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I didn't know that dicts were usable in this situation!
11

The "ternary" operator in Python is an expression of the form

X if Y else Z 

where X and Z are values and Y is a boolean expression. Try the following:

print "one" if a==1 else "two" if a==2 else "none" 

Here, the value of the expression "two" if a==2 else "none" is the value returned by the first when a==1 is false. (It's parsed as "one" if a == 1 else ( "two" if a==2 else "none").) It returns one of "one", "two", or "none", which is then passed as the sole argument for the print statement.

4 Comments

@avaragecoder, your code is polluted with "print" statements, that doesn't work. This works as expected.
@xbello Oh. In Ruby that is the same and I guess most of my code related to Ruby.., it was the same. Thank you for the information!
Note that I state X and Z are values, not statements.
Note that this is a bad side of one-liners. You got a "Syntax Error" and didn't know where it came from. Blamed the "X if Y else Z" but your error was elsewhere.
5

Use nested condtional expressions (ternary operator):

>>> a = 2 >>> print 'one' if a == 1 else 'two' if a == 2 else 'none' two 

Comments

4

I would use a dict instead of nested if

options = {1: "one", 2: "two"} print options.get(a, 'none') 

2 Comments

Where is the option of 'none'?
There it is :p, on the second parameter of get you can put the default value if the key is not found
2

Use a tuple index and conditional:

print ('one', 'two', 'none')[0 if a==1 else 1 if a==2 else 2] 

Alternatively, if a's relationship to an index can be an expression:

print ('one', 'two', 'none')[a-1 if a in (1,2) else -1] 

You can also combine the tuple index method with a dict to produce the index to get something a bit more readable than a straight dict approach (IMHO):

print ('one', 'two', 'none')[{1:0,2:1}.get(a, -1)] 

1 Comment

+1; this is a not horrible way to do something I would never want to.
0
print "one" if a == 1 else "two" if a == 2 else "none" 

Comments

0
print "one" if a == 1 else("two" if a ==2 else "None") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.