0

I know I can do this:

z = a if switch == 1 else z = b

but what if I want z=a if switch=1, z=b if switch=2, and z=c if switch=3? Is there a python valid way to write this as a single line?

something like:

z = a if switch == 1 else z = b if switch == 2 else z = c

Thanks, just learning Python now (obviously).

4
  • FWIW, z = a if switch == 1 else z = b is not valid since the assignment z = b is a statement, not an expression. I think you mean z = a if switch == 1 else b :-) Commented Aug 9, 2015 at 20:02
  • This is a recipe for confusing code, which is not Pythonic. If you're got something more complicated than a single ternary operator can handle, don't use ternary operators. Commented Aug 9, 2015 at 20:08
  • Or, in the case of python, a "conditional expression" which is the equivalent to a ternary operator in other languages :-). Commented Aug 9, 2015 at 20:22
  • mgilson, thank you, yes indeed - sorry to be so new to this all! jwilner - I don't disagree, this is more a question of what is possible, not what is recommended. ;) Commented Aug 9, 2015 at 21:13

2 Answers 2

5

Like this:

z = a if switch == 1 else (b if switch == 2 else c)

And you can keep going, but you didn't say what to use after c if it wasn't 3...

Sign up to request clarification or add additional context in comments.

5 Comments

You technically don't need the parens, but personally I think it's a lot more readable with :P
Oh wow, I actually imagined that this was not doable - it's so cool to see that! Would you just keep nesting parens with more options? z = a if switch == 1 else (b if switch == 2 else (c if switch == 3 else d)) for example?
@SindyR Yes, but at some point, you might want to rethink your strategy, and use a dict lookup or something :)
I am sure, this is likely not Pythonic at all, but I was curious to see what was possible. :)
@NightShadeQueen True. Interestingly, z = a if b else c if d else e is accepted by the compiler even though it's ambiguous (you have to know that it is interpreted the way I wrote it instead of z = (a if b else c) if d else e -- but z = a if b if c else d else e is a syntax error even though it is unambiguous.
1

if a, b, c, ... are static, then you might as well just use a dict:

lookup = {1: a, 2: b, 3: c}
z = lookup[switch]

It'd probably be most efficient if you could generate dict only once and reuse it every time. Also note that since switch seems to be sequential integers starting at 1, you could also use a list (or tuple):

lookup = (a, b, c)
z = lookup[switch - 1]

1 Comment

thanks, I'll keep this in mind - I am sure that a dict in this case may be more Pythonic, but I like knowing what functions in Python even when idiom doesn't recommend it. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.