Home > Net >  How to spread for loop target list across multiple lines
How to spread for loop target list across multiple lines

Time:06-10

I'm having trouble with unpacking a large number of variables from list of tuples. How can I make the unpacked variables span multiple lines?

I want to change

for t,a,f,b,s,x,j,p,w,q in results:
    print(t,s,x)

to

for t,a,f,b,s,x,
    j,p,w,q in results:
    
    print(t,s,x)

CodePudding user response:

Another option (with round brackets):

for (t, a, f, b, s, x, 
     j, p, w, q) in results:
    print(t, s, x)

CodePudding user response:

You can use \ at the end of each line, to spread your python code across the multiple lines.

for t,a,f,b,s,x, \
    j,p,w,q in results:
    
    print(t,s,x)

CodePudding user response:

You could just ask Black. When you have something actually too long for one line (i.e., like your "the actual variable names are a lot longer", not like your question's example which doesn't show the issue), it uses parentheses.

  • Related