3

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

Markus84612
  • 191
  • 1
  • 3
  • 11
  • 2
    Possible duplicate of [Python list subtraction operation](https://stackoverflow.com/questions/3428536/python-list-subtraction-operation) – Advay Umare Jan 13 '18 at 17:44

6 Answers6

3

You can do a list comprehension..

[x for x in listA if x not in listB]
Advay Umare
  • 432
  • 10
  • 23
1

Try this

>>> def li(li1,li2):
    li3=li1
    for i in li2:
        if i in li1:
            li3.remove(i)
    return(li3)

>>> li(["G","A","H","I","J","B"],["A","B","C"])
['G', 'H', 'I', 'J']
Artier
  • 1,648
  • 2
  • 8
  • 22
1

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.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • As the documentation you link warns right at the start, the sets module has been deprecated since Python 2.6, which was released almost ten years ago (1 October 2008), and doesn't even exist in modern Python. – DSM Jan 13 '18 at 17:43
  • @DSM Good catch on the documentation. Have this instead: https://docs.python.org/3/tutorial/datastructures.html#sets – Woody1193 Jan 13 '18 at 17:47
  • I don't want the duplicates in listA to be removed only the intersection between listA and listB, so we can't use the set() function. – Markus84612 Jan 13 '18 at 18:32
  • @Markus84612 I'm not sure I understand your meaning. If you want the intersection between sets A and B, just do `setA & setB`. If you want to keep set A pristine, just copy the list before the operation – Woody1193 Jan 13 '18 at 18:42
0

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:
        print(i)
Kaustubh.P.N
  • 229
  • 1
  • 8
0

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

https://docs.python.org/3/tutorial/datastructures.html#sets

>>> 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'}
Community
  • 1
  • 1
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • The set() removes the duplicate elements in the listA as well. I don't want that, I just want the intersection of the 2 lists to be removed. – Markus84612 Jan 13 '18 at 18:31
0
list1 = ['string1','string2','string3']
list2 = ['string1','string2','string3','pussywagon'] 

newList = list(set(list2)-set(list1))

# output
print(newList)

# type
print(type(newList))

test code

kvhrs
  • 3
  • 3