0

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?

1

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.