Python list subtraction operation

last modified : 2016-06-01 | published: 2015-04-07 | category:

a = [1, 2, 3, 4, 4, 5]
b = [3, 4]

How to get result:

a - b = [1, 2, 4, 5]

incorrect 1:

list(set(a) - set(b))
[1, 2, 5]

incorrect 2:

[i for i in a if i not in b]
[1, 2, 5]

correct:

def list_sub(list1, list2):
    for i in list1:
        if i in list2:
        list1.remove(i)
        list2.remove(i)
        return list1