I want to write a statement that breaks out of a for-loop if a certain condition is fulfilled, but in one line.
I know that this works:
for val in "string":
if val == "i":
break
print(val)
and I know that this works:
value_when_true if condition else value_when_false
but when I run this piece of code I get a syntax error:
break if some_string[:5] == condition
Is there a way to write such a break condition in one line? Am I maybe doing something wrong?
Thanks!
break
on the same line as the condition:if val == "i": break
. – Roughen