add all variable to list, then break that list into smaller list

Viewed 28

when i try to add c to the list w, i want there to print only one list, but for some reason it prints several

for i in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
total=0
w=[]
for i in range(n):
    c=b[i]-a[i]
    if c<0:
        c=c*-1
    else:
        c=c*1
    w.append(c)
    print(w)

this is the code, and i thought that if i appended c to the empty list, it would return only one list. but instead it prints

[1]
[1, 1]
[1]
[1, 1]
[1, 1, 1]
[1]
[1, 1]
[1, 1, 2]

i want to know why, and how to print it all in one list, and then when i have that one list to break it up into 3 separate lists who's lengths are the variable n thx so much for help

1 Answers

move the print func out the while loop:

for i in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
total=0
w=[]
for i in range(n):
    c=b[i]-a[i]
    if c<0:
        c=c*-1
    else:
        c=c*1
    w.append(c)
print(w)
Related