-1
def array_diff(a, b):     
    b_index=0 
    while b_index < len(b):
        for x in range(len(a)): 
            if b[b_index]=a[x]:  
                del a[x]
        b_index+=1
    print(a)

array_diff([1,2,3,4,5,6,6,7],[1,3,6])

causes a runtime error because i am mutating the container while still iterating over it what exactly will be the best practice to delete the matching items and return the list without the items

Initial List [1,2,3,4,5,6,6,7]
Final List [2,4,5,7]
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
Atikpui7
  • 3
  • 2

1 Answers1

0

Here is how you can return the result in a new list:

elements = [1, 2, 3, 4, 5, 6, 6, 7]
exclude = [1, 3, 6]
result = [e for e in elements if e not in exclude]
# result == [2, 4, 5, 7]
oebilgen
  • 41
  • 4