Appending the removed elements to a list in Python

Advertisements

I am removing J1 from J but I also want to append the two. I present the current and expected output.

J=[2, 6, 9, 10]
J1=[6,10]
for i in range(0,len(J1)): 
    J.remove(J1[i])
    J.append(J)
print(J)

The current output is

[2, 9, [...], [...]]

The expected output is

[[2, 6, 9, 10],[2,9]]

>Solution :

l=[]
l.append(J)
l.append([x for x in J if x not in J1])

print(l)
#[[2, 6, 9, 10], [2, 9]]

Leave a ReplyCancel reply