I'm trying to implement a difference function, which subtracts one list from another and returns the result. Here is what i have so far:
def array_diff(a, b):
for e in b[:]:
for i in a:
a.remove(i)
return a
in1 = [1, 2, 2]
in2 = [1]
print(array_diff(in1, in2))
I have two sample tests that i'd like to run.
Test.assert_equals(array_diff([1,2,2], [1]), [2,2], "a was [1,2,2], b was [1], expected [2,2]")
Test.assert_equals(array_diff([1,2,2], [2]), [1], "a was [1,2,2], b was [2], expected [1]")
How would i be able to remove the same value more than once?
asked Feb 14, 2020 at 19:58
-
Does this answer your question? Delete intersection between two listsEdeki Okoh– Edeki Okoh2020年02月14日 20:04:57 +00:00Commented Feb 14, 2020 at 20:04
1 Answer 1
array_diff = lambda a, b: [ i for i in a if i not in b]
array_diff(in1,[1])
[2, 2]
array_diff(in1,[2])
[1]
answered Feb 14, 2020 at 20:14
lang-py