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
🌐
sebhastian
sebhastian.com › python-one-line-if-without-else
One line if without else in Python | sebhastian
February 22, 2023 - By returning None, Python will do nothing when the else statement is executed. Although this one line if statement works, it’s not recommended when you have an assignment in your if body as follows:
Discussions

If statement, no elif, no else.
Should work just fine. If the condition isn't true, then nothing happens, and the next thing starts. More on reddit.com
🌐 r/learnpython
6
1
June 20, 2017
Making a single-line 'if' statement without an 'else' in Python 3 - Stack Overflow
1 Am I right in thinking there is no way to put an if statement and an else statement on one line in Python? ... How to fix the message qt.svg: /path/to/icon.svg:150:32: Could not add child element to parent element because the types are incorrect? Is my design correct for serial via USB and Ethernet shield ground? ... I have a character that has a godly wolf pet, but it makes up his whole identity. Does anyone know a way to kill the wolf without ... More on stackoverflow.com
🌐 stackoverflow.com
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
python - Putting a simple if-then-else statement on one line - Stack Overflow
How do I write an if-then-else statement in Python so that it fits on one line? For example, I want a one line version of: if count == N: count = 0 else: count = N + 1 In Objective-C, I wo... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 1: One-Liner If if condition: print('hi') # hi · This method is perfectly valid and you could see it in practice. Yet, I have to mention that it “violates” the PEP8 standard (multiple statements in a single line). Therefore, you shouldn’t consider this to be Pythonic code (there are worse things under the sun though).
🌐
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
Python Training ... 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 ...
🌐
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, we are comparing and finding the minimum number by using the ternary operator. ... Explanation: This is not a ternary expression, but rather a one-line if statement.
🌐
Reddit
reddit.com › r/learnpython › if statement, no elif, no else.
r/learnpython on Reddit: If statement, no elif, no else.
June 20, 2017 -

I'm curious about a certain section of some code i'm writing here-

I have a condition that adds additional work, but it is a very specific condition. Because it is so specific, I use an 'If' statement, with no else to follow up. Here is an example:

def run(self):
    while not self.stopper.isSet():
        try:
            ## Do some work
            
            if Identity.user == 'username1':
                ## Do extra work, specific to username1

            ## Continue usual work

        except self.queue.empty():
            continue

Notice no 'elif' or 'else' statement- I guess my question is, is there a better way to handle this? Or am I mistaken in assuming all 'If' statements need to be closed with 'else'?

Thanks!

EDIT: For context- Code deals with threading and queues, so it is important to get the 'username1' work done with the same item taken from the queue in the beginning of the 'Try' statement.

Top answer
1 of 5
3
Should work just fine. If the condition isn't true, then nothing happens, and the next thing starts.
2 of 5
3
Ifs are perfectly fine without an else. The indented or trailing statements are just dependent on the condition. Check for example this example from the Python tutorial that just checks a length value: if len(w) > 6: words.insert(0, w) Two other things though: while not self.stopper.isSet(): I'm guessing this could be a synchronization primitive like a threading.Event? That used to have the non-standard formatted isSet, but that has changed to is_set() since 2.6. Even though the old format still works there is a big chance it won't forever. except self.queue.empty(): continue self.queue.empty() is a method of a queue that returns a Boolean. It isn't an exception. You've mixed both workflows here. Either do while not self.stopper.is_set(): if self.queue.empty(): continue # otherwise get from queue or while not self.stopper.is_set(): try: item = self.queue.get_nowait() except queue.QEmpty: # <--- this is the real exception continue # otherwise get from queue But then a third problem: if the queue is empty, it will just keep trying and trying constantly, maybe even more than a 1000 times a second! That causes the CPU to reach 100% (or at least on one core) and the other threads to slow down. Always implement a slight delay to let the CPU attend other tasks: while not self.stopper.is_set(): if self.queue.empty(): sleep(0.1) continue # otherwise get from queue Or even use a longer pause if you don't expect new items to come in that often. Same is possible for the exception way, even better by implementing the regular queue.get's timeout parameter: while not self.stopper.is_set(): try: item = self.queue.get(timeout=0.1) except queue.QEmpty: # <--- will just fire 10 times a second continue # otherwise get from queue The benefit of this method is that it will never wait longer than necessary. If it even waited 0.01 second and then an item became ready in the queue, the function would continute at once (at least when the thread is running). In the case of using sleep, you are guaranteed that an empty queue condition will at least be maintained for the sleep duration.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › python one line if without else | example code
Python one line if without else | Example code - EyeHunts
August 23, 2021 - Python One-Liner If Statement example code if the body with only one statement, it’s just as simple as avoiding the line break. condition = True if condition: print('one line if without else')
🌐
Finxter
blog.finxter.com › home › learn python blog › if-then-else in one line python
If-Then-Else in One Line Python - Be on the Right Side of Change
September 23, 2022 - Yes, you can write most if statements in a single line of Python using any of the following methods: Write the if statement without else branch as a Python one-liner: if 42 in range(100): print("42").
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › How-can-we-use-Python-Ternary-Operator-Without-else
How can we use Python Ternary Operator Without else?
March 5, 2020 - 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 − ·
🌐
Quora
quora.com › Should-I-condense-my-if-else-statements-in-Python-into-one-line-even-if-it-does-not-make-the-code-any-more-readable
Should I condense my if-else statements in Python into one line even if it does not make the code any more readable? - Quora
Answer (1 of 6): This is the sort of question that leads to quasi-religious dogmatism in answers. On the one hand there is PEP-8 which says you should not condense – except that it also has the clause “unless it is better to do so” (paraphrased). All the code formatting checkers that realise PEP...
🌐
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.
🌐
Codecademy
codecademy.com › forum_questions › 526868e5548c352551000f60
"If" statement without the "else" | Codecademy
An if statement looks at any and every thing in the parentheses and if true, executes block of code that follows. If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed.
🌐
Sololearn
sololearn.com › en › Discuss › 2336541 › solved-how-to-use-for-loop-and-if-statement-in-one-line
[solved] How to use for loop and if statement in one line?? | Sololearn: Learn to code for FREE!
This will add only those items to the list that meet the condition (for which the condition is True). For example [i for i in range(8) if i%2!=0] Ans: [1, 3, 5, 7] and also you can nested conditionals. With a Python list comprehension, it doesn’t have to be a single condition; you can nest conditions.
🌐
W3Schools
w3schools.com › python › gloss_python_if_else_shorthand.asp
Python Shorthandf If Else
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... If you have only one statement to execute, one for if, and one for else, you can put it all on the same line: