I’ve been using a tuple as a substitution of a ternary operator in Python. Even though it is not exactly a ternary operator and has some drawbacks, but it works fine in most cases.
w = (u, v)[x < y]
x < y is true, the whole expression of the tuple,
(u, v)[x < y], will be evaluated as
v because true means 1 here. Likewise, false will cause the tuple expression to be evaluated as
I had another trick to use with lambda functions.
lambda w: x < y and v or u
This might be a little less obvious to see what’s going on. When
x < y is false, the following part,
and v, will not be evaluated due to short-circuit evaluation, and it will jump to the next part,
or u. Thus, this whole expression is to be evaluated as
u. Similarly, when
x < y is true,
and v part has to be evaluated so it the whole expression will be evaluated as
However, as acute readers might already noticed, this trick has a major drawback. When
v is a falsy value such as
None, the whole expression will be evaluated as
u regardless of the result of
x < y. So, one should refrain from using this trick unless it is impossible for
v to be a falsy value.
Last Saturday, Cody told me that there is a ternary operator equivalent in Python.
w = v if x < y else u
Strictly speaking, it is not a ternary operator, rather an expression. But, in my opinion, it is more intuitive thus it makes it easier to maintain the code.