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
Add `raise ... if ...` syntax to `raise` statement without `else` - Ideas - Discussions on Python.org
There was a discussion about this on StackOverflow, where the user asked how to implement the Samurai principle (Return victorious, or not at all.) It will be great if the such syntax was added for the raise statement (without needing the preceding else) Example: raise if not something raise ... More on discuss.python.org
🌐 discuss.python.org
0
November 23, 2022
🌐
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.
🌐
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.
🌐
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 ...
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')
🌐
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 − ·
🌐
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 - Python offers a simple and concise way to handle conditional logic by using inline if, also known as the ternary operator or conditional expression. Instead of writing a full if-else block, we can evaluate conditions and assign values in a single ...
🌐
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...
🌐
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").
🌐
Codecademy
codecademy.com › forum_questions › 526868e5548c352551000f60
"If" statement without the "else" | Codecademy
July 17, 2015 - 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.
🌐
Python.org
discuss.python.org › ideas
Add `raise ... if ...` syntax to `raise` statement without `else` - Ideas - Discussions on Python.org
November 23, 2022 - There was a discussion about this on StackOverflow, where the user asked how to implement the Samurai principle (Return victorious, or not at all.) It will be great if the such syntax was added for the raise statement (without needing the preceding else) Example: raise if not something raise X(*args, **kwargs) if error The such syntax can also be added for the return statement AFAIK this can be implemented even without changing the current grammar, but only adding the fallback route in AST...
🌐
Betterdatascience
betterdatascience.com › python-if-else-one-line
Python If-Else Statement in One Line - Ternary Operator Explained | Better Data Science
October 1, 2022 - Most programming languages require the usage of curly brackets, and hence the single line if statements are not an option. Other languages allow writing only simple conditionals in a single line. And then there’s Python. Before diving into If Else statements in one line, let’s first make a short recap on regular conditionals.
🌐
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.
🌐
Codingem
codingem.com › home › python if-else on one line
Python If-Else on One Line - codingem.com
May 17, 2023 - But you can get the job done by writing the if-else statement as a neat one-liner expression. ... As you can see, you were able to save three lines of code without sacrificing the code readability. Fitting everything in one line is not good practice. More importantly, a Python code line should ...
🌐
Sololearn
sololearn.com › en › Discuss › 3252109 › solved-python-oneliner-ifelse-statement
[Solved] Python one-liner if-else statement | Sololearn: Learn to code for FREE!
The question was how to write it in one line. ... Tibor Santa that last one is an innovative way of using index 0 or 1. False, True resolves to 0 and 1. It is the shortest if else statement I have ever seen. It also works for tuples c = (19,9)[a>b] or doing if else without writing if-else: opt = input() print(('you picked 1', 'something else')[opt!='1'])