0

I have a python script with removes element from list1 if they are present in list2. Seems preety simple. This is what I did:

arr1 = [1,2,3,4,5,6,7]
arr2 = [3,7,2,1,4,6]
for i in arr1:
    print i
    if i in arr2:
       arr1.remove(i)
       print arr1

Expected Output 5

Got this

1
[2, 3, 4, 5, 6, 7]
3
[2, 4, 5, 6, 7]
5
6
[2, 4, 5, 7]  # final output

Even after printing i at each iteration I can't figure out why it skips 2 in the first place. But when I repeated the loop 3 times I got expected output.

arr1 = [1,2,3,4,5,6,7]
arr2 = [3,7,2,1,4,6]
for i in arr1:
    print i
    if i in arr2:
       arr1.remove(i)
       print arr1
for i in arr1:
    print i
    if i in arr2:
       arr1.remove(i)
       print arr1
for i in arr1:
    print i
    if i in arr2:
       arr1.remove(i)
       print arr1 

Output

1
[2, 3, 4, 5, 6, 7]
3
[2, 4, 5, 6, 7]
5
6
[2, 4, 5, 7] 
2
[4, 5, 7]
5
7
[4, 5]
4
[5]

This gives me 5 as output in the final iteration. Can someone tell whats going here. And why it didn't remove all elements at first place and why I need to run them 3 times.

Edit Although list comprehension worked:

arr1[:] = [x for x in arr1 if x not in arr2]  

But why isn't my first code working. It is doing the same as what the list comprehension doing it

Raghav Patnecha
  • 716
  • 8
  • 29
  • just to be clear will your list have any duplicate elements or not if yes then what you want to do. remove all or one? – Gahan May 01 '18 at 14:53
  • I am looking for a more generic solution here. I am asking why 3 iteration instead of how to. Although I did it by the list comprehension way and it worked but why isn't it working in the traditional way – Raghav Patnecha May 01 '18 at 14:54
  • @Gahan it doesn't have duplicate elements. Although list2 has almost same number of elements as list1 and I just want to remove those elements from list1 which are present in list2. List comprehension worked `arr1[:] = [x for x in arr1 if x not in arr2] ` but why isn't the traditional way working – Raghav Patnecha May 01 '18 at 14:59
  • list comprehension you commented is creating new list not removing from existing one – Gahan May 01 '18 at 15:01
  • So you are suggesting instead of removing elements form existing one I should create a new list and append to it – Raghav Patnecha May 01 '18 at 15:03
  • @Gahan so I created a new list and append the element not in list2 to it and it worked. Maybe there was some referencing issue under the hood – Raghav Patnecha May 01 '18 at 15:05
  • the issue is you are iterating over list and removing item from same list. so while iteration if you remove first element it means 2 is considered as first element of list and second step of iteration it will skip 2 because the list you are iterating is modified. – Gahan May 01 '18 at 15:09
  • @Gahan Exactly. That's why creating a new list worked. Now I got that – Raghav Patnecha May 01 '18 at 15:13

1 Answers1

-1

for i in arr2: for j in arr1: if i in arr1: arr1.remove(i) print(arr1)