Appending the removed elements to a list in Python

Question:

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]]
Asked By: isaacmodi123

||

Answers:

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

print(l)
#[[2, 6, 9, 10], [2, 9]]
Answered By: God Is One
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.