Python Nested Multiple Ternary Operators

Rate this post

In which order does the nested ternary operator evaluate its conditions?

Short Answer: The nested ternary operator '1' if x else '2' if y else '3' evaluates the condition from left to right, i.e., '1' if x else ('2' if y else '3'). In short, first condition first!

Python Nested Multiple Ternary Operators

Problem Formulation

Given a nested ternary operator in Python:

'1' if x else '2' if y else '3'

Question: What is the operator precedence for the nested ternary operator?

  • Scenario A: ('1' if x else '2') if y else '3'
  • Scenario B: '1' if x else ('2' if y else '3')

Scenario A is semantically different to Scenario B as can be seen in this example:

>>> x, y = True, False
>>> # Scenario A
>>> ('1' if x else '2') if y else '3'
'3'
>>> # Scenario B
>>> '1' if x else ('2' if y else '3')
'1'

Simple Nesting

The ternary operator evaluates from left to right. If the ternary operator is nested such as in '1' if x else '2' if y else '3', Python first evaluates the left condition before it evaluates the right condition.

>>> x, y = True, False

# Scenario A
>>> ('1' if x else '2') if y else '3'
'3'

# Scenario B
>>> '1' if x else ('2' if y else '3')
'1'

# Correct Scenario: B
>>> '1' if x else '2' if y else '3'
'1'

Multiple Nesting

What if the ternary operator has multiple nesting levels like so:

'1' if x else '2' if y else '3' if z else '4'

Again, the ternary operator evaluates from left to right. Even if the ternary operator has more than two nesting levels such as in '1' if x else '2' if y else '3' if z else '4', Python first evaluates the left condition before it evaluates the second-left condition before it evaluates the third-left condition.

You can see this in the following example:

>>> x, y, z = True, True, False

>>> # Scenario A
>>> (('1' if x else '2') if y else '3') if z else '4'
'4'

>>> # Scenario B
>>> '1' if x else ('2' if y else ('3' if z else '4'))
'1'

>>> # Correct Scenario: B
>>> '1' if x else '2' if y else '3' if z else '4'
'1'

How to Manipulate the Operator Precedence?

You can manipulate the normal operator precedence for the ternary operator by using the parentheses accordingly. By enclosing a term in a parentheses, you force Python to evaluate the expression in the specified order. This way, you can force Python, for example, to evaluate the second condition before the first condition of the ternary operator.

Here’s an example:

>>> x, y = True, False
>>> # Normal precedence: first condition first
>>> '1' if x else '2' if y else '3'
'1'
>>> # Force second condition first:
>>> ('1' if x else '2') if y else '3'
'3'

How Does the Ternary Operator Work Anyway?

The most basic ternary operator x if c else y consists of three operands x, c, and y. It is an expression with a return value. The ternary operator returns x if the Boolean expression c evaluates to True. Otherwise, if the expression c evaluates to False, the ternary operator returns the alternative y.

Syntax: The three operands are written as x if c else y which reads as “return x if c else return y“. Let’s write this more intuitively as:

<OnTrue> if <Condition> else <OnFalse>
OperandDescription
<OnTrue>The return expression of the operator in case the condition evaluates to True
<Condition>The condition that determines whether to return the <On True> or the <On False> branch.
<OnFalse>The return expression of the operator in case the condition evaluates to False

Operands of the Ternary Operator

Read more in our detailed guide here: