ternary conditionals


Terneries in Python work just fine, however nested ternaries can lead to code that looks convoluted.

Standard form conditional

 1i, j, r = 2, 3, 0
 2if i > 1:
 3 if j == 3:
 4     r = 4
 5 else:
 6     r = 5
 8 r = 6


1i, j = 2, 3
2r = (4 if j == 3 else 5) if i > 1 else 5

As illustrated above, ternaries have some syntactic issues in python, imo:


Our shortest version of the code above is:

1i, j = 2, 3
2r = (6, (5, 4)[j == 3])[i > 1]

We are converting the bool result of the conditional to an int (0 or 1) which we then use for tuple indexing. Please note we're using tuples rather than lists, since their immutability makes them presumably more efficient.

There are several advantages:

When not to use

It's worth noting that, unlike with regular ternaries, each expression found as elements in a list or tuple declaration actually gets evaluated. This means you ought to use these only for simple ternary expressions. Using the results of functions is a no-no, since the function would actually get evaluated, whether the result is used or not.

Chained comparisons

On the subject of unconventional conditionals, it's worth explicitly noting that chaining comparisons is fine. Write a comparison so that when x is between 5 and 10 inclusive, the program prints 'in bounds' else it prints 'out of bounds'.


1for x in range(15):
2  print(f'{x}: in bounds' if 5 <= x <= 10 else f'{x}: out of bounds')