How to use "continue" in single line if esle within a for loop
Asked Answered
P

6

6

I have a for loop and within that there is a simple single line if condition. I want to use continue option in the else part.

This does not work :

def defA() : 
    return "yes"
flag = False
for x in range(4) : 
    value = defA() if flag else continue
                              ^
SyntaxError: invalid syntax

Working code :

for x in range(4) : 
    if flag : 
        defA()
    else : 
        continue
Petrel answered 30/8, 2019 at 3:46 Comment(2)
X if C else Y is an expression. continue is a statements. Expressions in Python cannot contain statements. Transform your if expression into an if statement to fix.Smorgasbord
Ternary operator allow you to assign a variable to A if match condition else B. That's mean B should be a variable or value instead of a Python keyword (continue).Standoff
U
9

There is no such thing as a single line if condition in Python. What you have in your first example is a ternary expression.

You are trying to assign continue to a variable named value, which does not make any sense, hence the syntax error.

Underline answered 30/8, 2019 at 3:50 Comment(1)
@punkrockpolly It's a normal if statement, compressed into one line (which is bad style in Python world).Underline
O
2

Yes you can you continue statment in a single line as shown below. Let me know if you face some issue

for i in range(1, 5):
    if i == 2: continue
    print(i)
Overtax answered 13/1, 2023 at 7:13 Comment(0)
T
2
for x in range(4) : 
    if not flag: continue
    defA()

That's how I would do it. I like this guard-clause style pattern so the reader knows if there's no flag, there's other logic to worry about below and it avoids the extra indent.

Tonsillectomy answered 30/1, 2023 at 19:18 Comment(0)
A
1

Can you kindly explain a bit as what you are trying to achieve may be experts can suggest you a better alternative. To me it doesn't make sense to add else here because you are only adding else to continue-on, which will happen in anycase after the if condition.

In any case, if I get it right, what you are looking for is a list comprehension. Here is a working example of how to use it,

def defA() :
    return "yes"

flag = True

value = [defA() if flag else 'No' for x in range(4)]  #If you need to use else
value = [defA() for x in range(4) if flag]            #If there is no need for else
Amendment answered 30/8, 2019 at 4:20 Comment(2)
True, but it is safe to assume that this is an excerpt from the actual code, therefore we don't know if there are more lines after the if clause within the for loop.Underline
Agree, hence I first suggested him to explain the problem first rather discussing his solution. Just a suggestion though!Amendment
B
0

Python uses indent for control structures. What you want to do is not possible in python.

Benis answered 30/8, 2019 at 3:48 Comment(2)
This is not exactly correct, nor answers the question. You can write, for example if flag: foo(). Note the lack of indentation in the control structure.Underline
@Underline your answer was undoubtedly better, but my answer is still literally accurate. Python does use indent for control structures - and - what he wanted to do is not possible in Python. I did not intend to convey that there is no possible way to skip an indent occasionally like my favorite if DE:BUG(...) statement :)Benis
S
0

This worked for me:

array = [1,2,3,4,5]
filtered_array = [x for x in array if x > 3]
Shroyer answered 28/7, 2023 at 17:16 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Barden

© 2022 - 2025 — McMap. All rights reserved.