0

Hello I created two lists as the following:

listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2']
listB = ['A', 'B','C','D']

I want to remove all listB elements that are in listA. I did this:

array_length = len(listB)
for i in range(array_length):
    listA.remove(listB[i])

but it only removed only each element if there is duplicated ones. the output was

['1', '2', '3', 'A', 'D', '2']

my desired output is:

['1', '2', '3', '2]

4 Answers4

1

see below (using set)

listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2']
listB = ['A', 'B','C','D']
l = list(set(listA) - set(listB))
print(l)

output

['1', '3', '2']
balderman
  • 22,927
  • 7
  • 34
  • 52
0

To remove only the first occurrence of each of the B items from A:

>>> listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2']
>>> listB = ['A', 'B','C','D']
>>> for i in listB:
...     listA.remove(i)
...
>>> listA
['1', '2', '3', 'A', 'D', '2']

To remove all occurrences:

>>> listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2']
>>> listB = ['A', 'B','C','D']
>>> listA = [i for i in listA if i not in listB]
>>> listA
['1', '2', '3', '2']
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • oh that's what I get for reading the description, lol – Samwise Aug 28 '21 at 15:40
  • Using `list.remove()` indeed works, but is *not very efficient*. It will iterate over all of `listA` for every element in `listB`, so the time complexity is O(N^2). My approach, with a Counter, is O(N), linear time. Try to do this with lists with 1000s of elements and the approach becomes much, much slower. – Martijn Pieters Aug 28 '21 at 15:45
  • And, the OP switched their desired outcome. – Martijn Pieters Aug 28 '21 at 15:46
  • Samwise answer was helpful. thanks all – Samuel Jackson Aug 28 '21 at 15:49
  • ha, I got it right the first time! I'll just go ahead and have both answers in there and wear my -1 as a badge of honor for being ahead of my time. – Samwise Aug 28 '21 at 15:52
  • i tried to vote up but it said i need at least 15 reputation to vote – Samuel Jackson Aug 28 '21 at 15:56
  • re: efficiency, I'll note that for small lists, over-engineering the solution might end up actually increasing the runtime, and in any case it increases the maintenance cost of the code. It's useful to know how to scale up a solution to arbitrarily large lists for the cases where the list is actually going to be arbitrarily large, but in practice you can do things like this the simple way about 95% of the time (and I say this having worked on a decent amount of enterprise software that does have scaling considerations -- even in those applications there's a *lot* of code that doesn't). – Samwise Aug 28 '21 at 15:57
0
listA = ['A', 'B','C','D','1','2','3', 'A', 'D','2']
listB = ['A', 'B','C','D']

for i in listB:
    while i in listA:
        listA.remove(i)

listA = list(dict.fromkeys(listA))

print(listA)

This code produces the required result

0

With new features is very easy.

const getItems = (a1,a2)=> {
   const leftArr = new Set([…a1]);

   return a2.filter(i => !leftArr.has(i);
}
Ernesto
  • 3,944
  • 1
  • 14
  • 29