Computer >> Computer tutorials >  >> Programming >> Python

How can I remove the same element in the list by Python


Just remove return statement outside for block. It will work. Also last print statement should have remove_same instead of remaove_new

def remove_same(L1, L2):
    L1_copy = L1[:]
    for e in L1_copy:
        if e in L2:
             L1.remove(e)
    return L1

L1 = [1,2,3,4]
L2 = [1,2,5,6]
print(remove_same(L1, L2))

The result:

[3, 4]