Yes, you can do this:

<condition> and myList.append('myString')

If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and the element will be appended.

I'll just point out that doing the above is quite non-pythonic, and it would probably be best to write this, regardless:

if <condition>: myList.append('myString')

Demonstration:

>>> myList = []
>>> False and myList.append('myString')
False
>>> myList
[]
>>> True and myList.append('myString')
>>> myList
['myString']
Answer from Claudiu on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › ternary-operator-in-python
Ternary Operator in Python - GeeksforGeeks
The ternary operator in Python perform conditional checks and assign values or execute expressions in a single line. It is also known as a conditional expression because it evaluates a condition and returns one value if the condition is True ...
Published   December 20, 2025
🌐
DataFlair
data-flair.training › blogs › python-ternary-operator
Python Ternary Operator - 5 Ways To Implement Ternary Operators - DataFlair
July 14, 2025 - Python Ternary Operator Tutorial with syntax and examples, what are Ternary Operator in Python,Python Ternary without else, Python If else
🌐
Mimo
mimo.org › glossary › python › ternary-operator
Python Ternary Operator: Syntax, Usage, and Examples
You can use the ternary operator to set a variable based on a simple condition: ... This is much shorter and cleaner than writing an if-else block. In Python programming, this approach is preferred when the logic is straightforward.
🌐
Python Tips
book.pythontips.com › en › latest › ternary_operators.html
6. Ternary Operators — Python Tips 0.1 documentation
The above example is not widely used and is generally disliked by Pythonistas for not being Pythonic. It is also easy to confuse where to put the true value and where to put the false value in the tuple. Another reason to avoid using a tupled ternery is that it results in both elements of the tuple being evaluated, whereas the if-else ternary operator does not.
Top answer
1 of 16
9350

Yes, it was added in version 2.5. The expression syntax is:

a if condition else b

First condition is evaluated, then exactly one of either a or b is evaluated and returned based on the Boolean value of condition. If condition evaluates to True, then a is evaluated and returned but b is ignored, or else when b is evaluated and returned but a is ignored.

This allows short-circuiting because when condition is true only a is evaluated and b is not evaluated at all, but when condition is false only b is evaluated and a is not evaluated at all.

For example:

>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'

Note that conditionals are an expression, not a statement. This means you can't use statements such as pass, or assignments with = (or "augmented" assignments like +=), within a conditional expression:

>>> pass if False else pass
  File "<stdin>", line 1
    pass if False else pass
         ^
SyntaxError: invalid syntax

>>> # Python parses this as `x = (1 if False else y) = 2`
>>> # The `(1 if False else x)` part is actually valid, but
>>> # it can't be on the left-hand side of `=`.
>>> x = 1 if False else y = 2
  File "<stdin>", line 1
SyntaxError: cannot assign to conditional expression

>>> # If we parenthesize it instead...
>>> (x = 1) if False else (y = 2)
  File "<stdin>", line 1
    (x = 1) if False else (y = 2)
       ^
SyntaxError: invalid syntax

(In 3.8 and above, the := "walrus" operator allows simple assignment of values as an expression, which is then compatible with this syntax. But please don't write code like that; it will quickly become very difficult to understand.)

Similarly, because it is an expression, the else part is mandatory:

# Invalid syntax: we didn't specify what the value should be if the 
# condition isn't met. It doesn't matter if we can verify that
# ahead of time.
a if True

You can, however, use conditional expressions to assign a variable like so:

x = a if True else b

Or for example to return a value:

# Of course we should just use the standard library `max`;
# this is just for demonstration purposes.
def my_max(a, b):
    return a if a > b else b

Think of the conditional expression as switching between two values. We can use it when we are in a 'one value or another' situation, where we will do the same thing with the result, regardless of whether the condition is met. We use the expression to compute the value, and then do something with it. If you need to do something different depending on the condition, then use a normal if statement instead.


Keep in mind that it's frowned upon by some Pythonistas for several reasons:

  • The order of the arguments is different from those of the classic condition ? a : b ternary operator from many other languages (such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc.), which may lead to bugs when people unfamiliar with Python's "surprising" behaviour use it (they may reverse the argument order).
  • Some find it "unwieldy", since it goes contrary to the normal flow of thought (thinking of the condition first and then the effects).
  • Stylistic reasons. (Although the 'inline if' can be really useful, and make your script more concise, it really does complicate your code)

If you're having trouble remembering the order, then remember that when read aloud, you (almost) say what you mean. For example, x = 4 if b > 8 else 9 is read aloud as x will be 4 if b is greater than 8 otherwise 9.

Official documentation:

  • Conditional expressions
  • Is there an equivalent of C’s ”?:” ternary operator?
2 of 16
1028

You can index into a tuple:

(falseValue, trueValue)[test]

test needs to return True or False.
It might be safer to always implement it as:

(falseValue, trueValue)[test == True]

or you can use the built-in bool() to assure a Boolean value:

(falseValue, trueValue)[bool(<expression>)]
🌐
sebhastian
sebhastian.com › python-one-line-if-without-else
One line if without else in Python | sebhastian
February 22, 2023 - You can use the ternary operator to write a one line if statement without an else as follows: ... By returning None, Python will do nothing when the else statement is executed.
🌐
Finxter
blog.finxter.com › home › learn python blog › python one line if without else
Python One Line If Without Else - Be on the Right Side of Change
July 18, 2020 - condition = True # Method 3: Ternary with Dummy for Assignment x = 42 if condition else None · If the condition does not hold, the “dummy” value None is assigned to the variable. This method I like most. It uses a Python optimization called “short circuiting” for Boolean operators: the logical and operator simply returns the second operand if the first is True.
Find elsewhere
🌐
Quora
quora.com › Why-should-you-use-a-Python-ternary-operator-instead-of-an-if-else-statement
Why should you use a Python ternary operator instead of an if-else statement? - Quora
Answer: Why should you use a Python ternary operator instead of an if-else statement? Should you? In general, not just in python. And as a rule of thumb, python’s ternary looks different than C’s, and PHP’s, so if you ask me, the exit is right where you came from, so please leave the room with t...
🌐
Dataquest
dataquest.io › blog › python-ternary-operator
Python Ternary: How to Use It and Why It's Useful (with Examples)
March 6, 2023 - The Python ternary operator (or conditional operator), tests if a condition is true or false and, depending on the outcome, returns the corresponding value — all in just one line of code.
🌐
CodeSpeedy
codespeedy.com › home › ternary operator without else in python
Ternary Operator Without Else In Python - CodeSpeedy
February 14, 2020 - In this tutorial, we are going to learn about how to implement a ternary operator without using the else keyword in Python.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-use-python-ternary-operator-without-else
How can we use Python Ternary Operator Without else?
PythonProgramming · If you want to convert a statement like − · if <condition>: <some-code> to a single line, You can use the single line if syntax to do so − · if <condition>: <some-code> Another way to do this is leveraging the short-circuiting and operator like − · <condition> and <some-code> If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated.
🌐
Sentry
sentry.io › sentry answers › python › does python have a ternary conditional operator?
Does Python Have a Ternary Conditional Operator? | Sentry
If so, what is the structure of a Python ternary operator? The ternary operator is used to shorten the code needed to write if-else blocks.
🌐
Flexiple
flexiple.com › python › python-ternary
Python ternary operators - How to use them? - Flexiple Tutorials - Flexiple
As the name suggests, Python’s ternary operators need three operands to run. The three operands are: - condition: A boolean expression that needs to evaluate whether true or false. - val_true: A value that is to be assigned if the condition evaluates to be true. - val_false: A value that is to be assigned if the condition evaluates to be false. When it’s all put together this is how it should look like: some_var = val_true if [condition] else val_false
🌐
Python Morsels
pythonmorsels.com › ternary-operator
Python's ternary operator - Python Morsels
November 14, 2022 - Python doesn't have the ?:-style ternary operator that many programming languages do. Instead, we have conditional expressions which read a little bit more like English and look kind of like an if-else statement all on one line of code.
🌐
Medium
medium.com › @ayush-thakur02 › all-about-ternary-operator-you-should-know-8b9d4a07b1cb
All About Ternary Operator you should know! | by Ayush Thakur | Medium
November 13, 2023 - A: No, the ternary operator requires both the value_if_true and the value_if_false parts. If you want to use a conditional expression without the else part, you can use the logical operators (&&, ||, and, or, etc.) instead. For example, in Python, ...
🌐
freeCodeCamp
freecodecamp.org › news › python-tenary-operator
Python Ternary Operator – Conditional Operators in Python
April 26, 2023 - It's a shorter way of writing if and if...else statements. You can use ternary operators to execute code based on predefined conditions. Happy coding! I also write about Python on my blog.
🌐
Hackr
hackr.io › home › articles › programming
Python Ternary Operator: How and Why You Should Use It
January 30, 2025 - Enter, the Python ternary operator. What is this? I hear you ask. Well, you can use the ternary operator to test a condition and then execute a conditional assignment with only one line of code. And why is this so great? Well, this means we can replace those ever-popular if-else statements ...
🌐
DataCamp
datacamp.com › tutorial › pythons-ternary-operators-guide
Python's Ternary Operators Guide: Boosting Code Efficiency | DataCamp
May 17, 2024 - A ternary operator is a conditional expression that evaluates a condition and returns one of two values based on whether the condition is true or false. The syntax of a ternary operator in Python is value_if_true if condition else value_if_false.
🌐
Python Guild
pythonguild.dev › home › tutorial › control flow › ternary operator
Python Ternary Operator: Write Cleaner One-Line Conditionals
No, it’s not allowed to omit the else part in Python’s ternary operator. The syntax requires both outcomes — one for True and one for False. If you write only value if condition, Python will raise a SyntaxError. This differs from some other languages where you can write short if expressions without an else.