Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Python: Ternary Operator / Conditional Expression

📅 2009-Oct-15 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ conditional expression, python, ternary operator ⬩ 📚 Archive

Conditional expression is Python's version of the C ternary operator (?:)

# Read PEP 308 for this design choice.

# Lots of lines for a simple initialization
alist = []
if solved:
    alist = solvedList
else:
    alist = problemList

# Conditional expression to achieve the above
alist = solvedList if solved else problemList