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 
2
  • 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. Commented Aug 30, 2019 at 3:52
  • 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). Commented Aug 30, 2019 at 3:52

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@punkrockpolly It's a normal if statement, compressed into one line (which is bad style in Python world).
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) 

Comments

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.

Comments

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 

2 Comments

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.
Agree, hence I first suggested him to explain the problem first rather discussing his solution. Just a suggestion though!
0

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

2 Comments

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.
@Selcuk 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 :)
0

This worked for me:

array = [1,2,3,4,5] filtered_array = [x for x in array if x > 3] 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.