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]
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]
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)
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]