I'm remaking this post to be more clear about my problem
Asked
Active
Viewed 52 times
-1
-
3this ==> [i for i in A if i not in B]. – Frayal May 16 '19 at 09:52
-
Also https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another – Andras Deak -- Слава Україні May 16 '19 at 09:55
-
Sorry for not being clear, but this solution doesn't work for me, I just don't see why – Jorge Gill May 16 '19 at 10:17
-
Whitespace around your strings. Use `.strip()` on them. Or add a reliable example of your data. – Andras Deak -- Слава Україні May 16 '19 at 13:24
1 Answers
1
You could use a list comprehension to create a new list. With the following code you're checking whether an element x
from A
is in B
. Every time x
is in B
, it is not included in your new list C
.
A = [1, 1, 1, 2, 3, 4, 4, 5, 5, 5, 5]
B = [1, 3, 4]
C = [x for x in A if x not in B]
print(C)
outputs:
[2, 5, 5, 5, 5]

trotta
- 1,232
- 1
- 16
- 23
-
This soution doesn't work for me. In reality, A has 545332 string elements and B has 23. When running C = [x for x in A if x not in B], C elements are the same as B, substraction didn't happen – Jorge Gill May 16 '19 at 10:18