def array_diff(a, b):
result = []
i = 0;
j = 0;
if (a[i] == b[j]):
a = a.remove(a[i]) #append() method in python adds a single item to the existing list
if i<len(a):
i+=1;
return a;
if j<len(b):
i+=1;
return a;
It should remove all values from list a, which are present in list b. If I should give an example:
array_diff([1,2],[1]) == [2]
array_diff([1,2,2,2,3],[2]) == [1,3]
I am very new to Python language, so I would appreciate any help. What am I doing wrong and how can I make it better?
I edited:
def array_diff(a, b):
b=set(b)
for v in a:
if v in b:
a.remove(v)
return list(a)
However, when a was [1,2,2], b was [2], expected [1]: it returns [1, 2] while it should equal [1]