pythonpython-3.xlistsubtraction

Subtracting one list from another in python where duplicate values may occur


I am looking to remove the contents of one array from another.

array_2 = ['one' , "two" , "one", "three", "four"]
array_1 = ['one', "two"]

My first thought was to use list comprehensions

array_3 = [x for x in array_2 if x not in array_1]

However this will remove the duplicate item result : ['three', 'four']

I want to only remove "one" once from the array as I am looking for a list subtraction. So I want the result to be : ['one', 'three', 'four'].

What is a good pythonic way to achieve this ?


Solution

  • I am just going to collect the excellent solutions already given above.

    If you care about maintaining the original order in which the elements in array_2 were, then I think you have to use remove:

    array_1 = ['one', 'two']
    array_2 = ['one', 'two', 'one', 'three', 'four']
    array_3 = list(array_2)
    for x in array_1:
        try:
            array_3.remove(x)
        except ValueError:
            pass
    print(array_3)
    

    If it does not matter what the final order of elements is, then using Counter is much more efficient as it only loops over both lists once:

    from collections import Counter
    
    array_1 = ['one', 'two']
    array_2 = ['one', 'two', 'one', 'three', 'four']
    array_3 = list((Counter(array_2) - Counter(array_1)).elements())
    print(array_3)