vendredi 18 juin 2021

List comprehension & ternary operator -> SyntaxError

With text, type, scope being string and val, altval being int, why is the following code syntatically not correct? (I know this isn't the correct way to do it aesthatically but would that affect syntax?)

result = [(i[val:] if scope=="before" else i[:val] if scope=="after" else i[val:altval] if scope=="beforeafter" else i) if j<=until for j,i in enumerate(text.split("\n"))]

Broken down into lines:

result = [ 
(i[val:] if scope=="before"
else i[:val] if scope=="after"
else i[val:altval] if scope=="beforeafter"
else i) if j<=until
for j,i in enumerate(text.split("\n"))]

With lines split up as this, the SyntaxError is at the last line:

for j,i in enumerate(text.split("\n"))]
^

Version: Python 3.x
System: Windows

What am I missing? Is it a list comprehension limitation?

Aucun commentaire:

Enregistrer un commentaire