-2

I have list like this:

a = [1,2,3,4,5,6,7]
b = [10,11,13,2,14,7]

I want output like this:

b = [10,11,13,14]

if an element of a is in b then it has been discarded. please, anyone can tell me how to do this?

joey
  • 345
  • 1
  • 2
  • 9

1 Answers1

2

Using list comprehension:

b = [x for x in b if x not in a]

Works like this:

a = [1,2,3,4,5,6,7]
b = [10,11,13,2,14,7]
b = [x for x in b if x not in a]

print b
>> [10, 11, 13, 14]

Re: @DeepSpace's suggestion, looking for elements of a set in a list will go significantly faster than looking for elements of a list in another list, so declare a as a set()

a = set([1,2,3,4,5,6,7])
b = [10,11,13,2,14,7]
b = [x for x in b if x not in a]
kamses
  • 465
  • 2
  • 10