Subtract 2 lists by duplicate elements in python

Hello I want to know how to subtract 2 lists by duplicate elements, not by value, in python.

ListA = [G, A, H, I, J, B]

ListB = [A, B, C]

ListC = [G, H, I, J] 

So we subtract the ListB values, if they are found in ListA as duplicates, and the ListC will give back the non-duplicate values in ListA.

  • Mathematically written it would be:

    ListC = ListA - (ListA ∩ ListB)

(I don't want to remove the duplicates in ListA, only the intersection between ListA and ListB, as described in the above formula, so this question is not a duplicate of questions/48242432

6 answers

  • answered 2018-01-13 17:35 Artier

    Try this

    >>> def li(li1,li2):
        for i in li2:
            if i in li1:
    >>> li(["G","A","H","I","J","B"],["A","B","C"])
    ['G', 'H', 'I', 'J']

  • answered 2018-01-13 17:36 advay.umare

    You can do a list comprehension..

    [x for x in listA if x is not in listB]

  • answered 2018-01-13 17:39 Woody1193

    Use the sets library in Python.

    from sets import Set
    setA = Set(['G', 'A', 'H', 'I', 'J', 'B'])
    setB = Set(['A', 'B', 'C'])
    # get difference between setA and intersection of setA and setB
    setC = setA - (setA & setB)

    The cool thing about sets is that they tend to operate faster than list comprehensions. For instance, this operation would tend to run at O(len(setA)) + O(min(len(setA), len(setB))) = O(len(setA)) whereas a list comprehension would run at O(len(setA) * len(setB)) to achieve the same result. Of course, these are average cases not worst cases. Worst case, they'd be the same. Either way, you should use the object that best fits your operations, right?

    See the Python documentation for more.

  • answered 2018-01-13 17:39 Kaustubh.P.N

    This is what you want?

    L1 = ['A', 'G', 'H', 'I', 'J', 'B']
    L2 = ['A', 'B', 'C']
    for i in L1:
        if i not in L2:

  • answered 2018-01-13 17:42 Patrick Artner

    On basis of using mathematical set notations, why not use sets?

    ListA = [G,A,H,I,J,B]
    ListB = [A,B,C]
    SetC = set(ListA) - set(ListB)

    But then you get sets out and have to go back to lists... also the order might change and any character that was twice in the list is then only once in it

    >>> a = set('abracadabra') # sets have only unique elements and are unordered
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}

  • answered 2018-01-13 19:36 Darkonaut

    list_a = ["G", "A", "H", "I", "J", "B"]
    list_b = ["A", "B", "C"]
    [i for i in list_a if i not in set(list_a).intersection(list_b)]
    # ['G', 'H', 'I', 'J']