That's more specifically a ternary operator expression than an if-then, here's the python syntax

value_when_true if condition else value_when_false

Better Example: (thanks Mr. Burns)

'Yes' if fruit == 'Apple' else 'No'

Now with assignment and contrast with if syntax

fruit = 'Apple'
isApple = True if fruit == 'Apple' else False

vs

fruit = 'Apple'
isApple = False
if fruit == 'Apple' : isApple = True
Answer from cmsjr on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › one-liner-for-python-if-elif-else-statements
Python If Else in One Line - GeeksforGeeks
July 23, 2025 - else "Zero": If both conditions are False, it returns "Zero" as the default result. Python does not directly support a true one-liner for if-elif-else statements like it does for a simple if-else.
Discussions

return if else else if in one line
Why are you wanting to have it as one line. Reduces readability and makes it more complicated to fix. What you should be able to do is to have a nested statement. Run the first expression, else if the second expression passes use statement2 return statement1 if expression1 else (statement2 if expression2 else statement3) You could continue nesting if you wanted More on reddit.com
🌐 r/learnpython
6
6
August 8, 2021
single line if statement
Yes you can do single line ternaries in python. I'm not sure how "pythonic" it is to other developers, but I appreciate its brevity. the syntax is: expr if cond else expr in your case rowsTot = len(content) if not rows else rows or if you're explicitly checking None: rowsTot = len(content) if rows is None else rows to really rewrite it best i'd do something like rowsTot = rows if rows else len(content) More on reddit.com
🌐 r/learnpython
11
13
March 8, 2017
If-else condition in one liner
for x in lst: _prev = x if x else prev What could why do you need x to be overwritten before it’s next loop…can now just use prev for the rest of the code block. x, _prev = (x,x) if x else (_prev, _prev) This should illuminate though why it’s seem pretty ridiculous to need. And give you a direct answer yes we can do this if we really wanted. But x won’t change the list, to do that we should….just make a new list. new_replace = [] _prev = None for x in my_list: _prev = x if x else _prev #do_something(_prev) new_replace.append(_prev) We can one line this…well two line technically, maybe if the first value is never empty we can omit, not sure. _prev = None _replace = [(_prev := x if x else _prev) for x in my_list] Pretty straightforward use of the walrus operation. (All we are doing is assigning and returning at the same time.) At this point you can just throw walrus into whatever function itself. And get right to it. And forget about it lists and stuff. _prev = None for x in my_list: no_empty_function(_prev := x if x else _prev) This is actually a fairly fast progression of a concept. And IMHO significantly “one lined”. (Though we can go further.) Or some enumerate() stuff but it honestly screams walrus function. I don’t need to make indexes and reference them, I already used the last value I just use it again. All that referencing get troublesome with huge datasets. Now my_list can be my_generator. More on reddit.com
🌐 r/learnpython
29
1
April 17, 2023
Does anybody else just not like the syntax for nested one-line list comprehension?
I agree - I find the nested syntax difficult to sort out every time I want to use it. But I know others who find it very natural. More on reddit.com
🌐 r/Python
98
354
July 31, 2022
🌐
LearnPython.com
learnpython.com › blog › python-if-in-one-line
How to Write the Python if Statement in one Line | LearnPython.com
This is the exact use case for a conditional expression! Here’s how we rewrite this if/else block in a single line: age = 8 is_baby = True if age < 5 else False print(is_baby) # output: # False · Much simpler! We hope you now know many ways to write a Python if in one line.
🌐
W3Schools
w3schools.com › python › python_if_shorthand.asp
Python Shorthand If
If you have only one statement to execute, you can put it on the same line as the if statement. ... Note: You still need the colon : after the condition. If you have one statement for if and one for else, you can put them on the same line using ...
🌐
Sololearn
sololearn.com › en › Discuss › 3252109 › solved-python-oneliner-ifelse-statement
[Solved] Python one-liner if-else statement | Sololearn: Learn to code for FREE!
Other oneliner options: d = 9 if a > b else 19 e = [19, 9][a > b] ... "One line" if statements in python are called ternary operator and it differs from traditional if,else blocks.
Find elsewhere
🌐
Codingem
codingem.com › home › python if-else on one line
Python If-Else on One Line - codingem.com
May 17, 2023 - In Python, you can create an if else on one line of code. For example: num = 0 if value
🌐
Python Central
pythoncentral.io › one-line-if-statement-in-python-ternary-conditional-operator
One line if statement in Python (ternary conditional operator) - Python Central
December 30, 2021 - So, when PEP 308 was approved, Python finally received its shortcut conditional expression: It first evaluates the condition; if it returns True, the compiler will consider expression1 to give the result, otherwise expression2. Evaluation is lazy, so only one expression will be executed. ... Here we have defined the age variable whose value is fifteen. Now we use the if-else command to print if the kid is an adult or not.
🌐
Sentry
sentry.io › sentry answers › python › python equivalent of a ternary operator: if-then-else in one line
Python equivalent of a ternary operator: if-then-else in one line | Sentry
It usually looks something like this: int x = b == true ? 1 : 0 // if b == true, x = 1; else x = 0 ... Yes, Python supports inline conditional assignments using a form of the ternary operator, usually called a conditional expression.
🌐
DEV Community
dev.to › kiani0x01 › python-one-line-if-else-statement-5817
Python One-Line If Else Statement - DEV Community
August 6, 2025 - Learn how to use Python's one-line if-else (ternary) expression for concise conditionals with examples, best practices, and tips.
🌐
Leapcell
leapcell.io › blog › how-to-use-python-if-else-in-one-line
How to Use Python `if...else` in One Line | Leapcell
July 25, 2025 - Use Python's one-line `if...else` for concise, readable conditional logic.
🌐
Python-bloggers
python-bloggers.com › 2022 › 01 › python-if-else-statement-in-one-line-ternary-operator-explained
Python If-Else Statement in One Line – Ternary Operator Explained | Python-bloggers
January 10, 2022 - You should be fine with two conditions in one line, as the code is still easy to read. The following example prints Go home. if age is below 16, Not Sure... if age is between 16 (included) and 18 (excluded), and Welcome otherwise: age = 17 outcome = 'Go home.' if age < 16 else 'Not sure...' if 16 <= age < 18 else 'Welcome' outcome
🌐
ONEXT DIGITAL
onextdigital.com › home › python if else in one line: a concise guide to use it and practical use cases
Python if else in one line: A concise guide to use it and Practical use cases
November 7, 2023 - This is achieved using a feature called a “conditional expression” or “ternary operator” in Python. The syntax of the one-class ternary operator is as follows: some_expression if condition else other_expression · Consider an if-else statement that determines if a person is an adult depending on their age: age = 20 if age < 18: age_group = "Minor" else: age_group = "Adult" print(age_group) This functions fairly well. However, putting an if-else statement as a tidy one-line expression would perform the job much better.
🌐
GeeksforGeeks
geeksforgeeks.org › python › different-ways-of-using-inline-if-in-python
Different Ways of Using Inline if (ternary operator) in Python - GeeksforGeeks
July 23, 2025 - <expression_if_true> if <condition> else <expression_if_false> ... Return Type: Result of expression_if_true if the condition is true, otherwise, result of expression_if_false. Let's explore the different ways of using Inline if: In this example, ...
🌐
Reddit
reddit.com › r/learnpython › if-else condition in one liner
r/learnpython on Reddit: If-else condition in one liner
April 17, 2023 -

There's this common syntax 'a' if condition else 'b' and I found it more readable and faster to write, but recently I ran into problem like this

# basically means if A exists, it covers the previous one, else prev becomes A. usually used within a loop.

lst = ['x', '', 'y']

prev = None
for x in lst:
    if x:
        prev = x
    else:
        x = prev

Somehow I cannot write this into prev = x if x else x = prev .

I kind of get it why this is an error but I just wonder if there's a better way to write this kind of code?

Top answer
1 of 5
3
for x in lst: _prev = x if x else prev What could why do you need x to be overwritten before it’s next loop…can now just use prev for the rest of the code block. x, _prev = (x,x) if x else (_prev, _prev) This should illuminate though why it’s seem pretty ridiculous to need. And give you a direct answer yes we can do this if we really wanted. But x won’t change the list, to do that we should….just make a new list. new_replace = [] _prev = None for x in my_list: _prev = x if x else _prev #do_something(_prev) new_replace.append(_prev) We can one line this…well two line technically, maybe if the first value is never empty we can omit, not sure. _prev = None _replace = [(_prev := x if x else _prev) for x in my_list] Pretty straightforward use of the walrus operation. (All we are doing is assigning and returning at the same time.) At this point you can just throw walrus into whatever function itself. And get right to it. And forget about it lists and stuff. _prev = None for x in my_list: no_empty_function(_prev := x if x else _prev) This is actually a fairly fast progression of a concept. And IMHO significantly “one lined”. (Though we can go further.) Or some enumerate() stuff but it honestly screams walrus function. I don’t need to make indexes and reference them, I already used the last value I just use it again. All that referencing get troublesome with huge datasets. Now my_list can be my_generator.
2 of 5
2
You can use or ( prev = x or prev ) or my personal favorite, a ternary operator: prev = x ? x : prev
🌐
GoLinuxCloud
golinuxcloud.com › home › python › how to use python if else in one line with examples
How to use python if else in one line with examples | GoLinuxCloud
December 31, 2023 - Learn how to use python if else statement in one line using ternary operator. You can also hack your way to use nested if else or if elif else in single line
🌐
Medium
medium.com › @BetterEverything › python-one-liner-if-else-statements-e47a715854da
Python one-liner if-else statements | by Better Everything | Medium
August 13, 2024 - result = 200 if result>0: result_text = 'Profit' else: result_text = 'Loss' print(result_text) This works fine and prints: Profit, but we can rewrite the middle part to make it 1 line of code instead of 4 by using a conditional expression.