To remove items displayed in both lists, use the following:
for i in a[:]: if i in b: a.remove(i) b.remove(i)
To create a function that does this for you, simply do:
def removeCommonElements(a, b): for e in a[:]: if e in b: a.remove(e) b.remove(e)
Or return new lists, rather than edit old ones:
def getWithoutCommonElements(a, b): # Name subject to change a2 = a.copy() b2 = b.copy() for e in a: if e not in b: a2.remove(e) b2.remove(e) return a2, b2
However, the former can be replaced with removeCommonElements as follows:
a2, b2 = a.copy(), b.copy() removeCommonElements(a2, b2)
To save a and b, but to create duplicates without common elements.
user1632861
source share