0

I would like to use one line to finish the for loop

But some thing wrong happen...

When I use the statement continue or break it gives the error

I create some easy code as a example:

l=[]
for i in range(3):
    for j in range(3):
        if i==j:
            l.append(i+j)
        else:
            break 

which the result would be

[0]

If I try to do it in one line,Here's my try:

list_= [i+j if i==j else break for i in range(3) for j in range(3) ]

And the error is raised: SyntaxError: invalid syntax

list_= [i+j if i==j else break for i in range(3) for j in range(3) ]
                               ^

Which I actually don't know how to fix it...

Can Somebody help me out? Thanks

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Pro_gram_mer
  • 749
  • 3
  • 7
  • 20

0 Answers0