-2

Implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b.

for example :-

a = [1,2,3,3,4]

b = [7,9,3]

the result should be [1,2,4]

2 Answers2

0

One way to get the result is to use list comprehension:

a = [1,2,3,3,4]
b = [7,9,3]

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

print(c)
liamsuma
  • 156
  • 4
  • 19
0

Simple list comprehension will solve it to you:

a = [1,2,3,3,4]
b = [7,9,4]
print([item for item in a if item not in b])

output:

[1, 2, 3, 3]
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7