In python, remove() is supposed to remove the first occurence of a value in the list. I tried the code below, however it doesn't remove all values found in list b from list a. Is there anything wrong with my code?
def array_diff(a,b):
for i in a:
if i in b:
a.remove(i)
return a
for example, it prints out [1, 2, 3] when it only should print [1, 3]
print(array_diff([1,2,2,2,3],[2]))
> [1, 2, 3]
additional note: I'm aware of a shorter version (shown below) that works as it should but I would like to know what's wrong with my code above? Thank you!!!
def array_diff(a, b):
a = list(i for i in a if i not in b)
return a