10

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?

ooransoy
  • 785
  • 5
  • 10
  • 25
  • 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? – Makoto Jan 02 '14 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. – ooransoy Jan 02 '14 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. – Gareth Latty Jan 02 '14 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](http://www.python.org/dev/peps/pep-0308/), which uses the same keywords and similar syntax, but is a fundamentally different operation with restrictions (primarily that it only supports expressions). – Gareth Latty Jan 02 '14 at 18:01
  • possible duplicate of [Ternary conditional operator in Python](http://stackoverflow.com/questions/394809/ternary-conditional-operator-in-python) – Makoto Jan 02 '14 at 18:26
  • 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. – ooransoy Jan 02 '14 at 18:32

7 Answers7

17

Try:

print {1: 'one', 2: 'two'}.get(a, 'none')
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
10

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 3
    @avaragecoder, your code is polluted with "print" statements, that doesn't work. This works as expected. – xbello Jan 02 '14 at 18:03
  • @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! – ooransoy Jan 02 '14 at 18:06
  • Note that I state `X` and `Z` are *values*, not *statements*. – chepner Jan 02 '14 at 18:11
  • 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. – xbello Jan 02 '14 at 18:15
5

Use nested condtional expressions (ternary operator):

>>> a = 2
>>> print 'one' if a == 1 else 'two' if a == 2 else 'none'
two
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
4

I would use a dict instead of nested if

options = {1: "one", 2: "two"}
print options.get(a, 'none')
Pablo Díaz Ogni
  • 1,033
  • 8
  • 12
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)]
dawg
  • 98,345
  • 23
  • 131
  • 206
0
print "one" if a == 1 else "two" if a == 2 else "none"
egl
  • 167
  • 1
  • 1
  • 8
0
print "one" if a == 1 else("two" if a ==2 else "None")
Janarthanan Ramu
  • 1,331
  • 16
  • 17