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
  • 48,339
  • 8
  • 43
  • 63
Pro_gram_mer
  • 519
  • 1
  • 5
  • 18

0 Answers0