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

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 3
    Does this answer your question? [Remove all the elements that occur in one list from another](https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another) – SuperStormer Sep 11 '20 at 21:11

1 Answers1

0
def diff(lst,lst2):
    lst=set(lst)
    for v in lst2:
        if v in lst:
           lst.remove(v)
    return list(lst)
print(diff([1,2,3],[3,5,7,8]))