Hi, let's say that I have two lists in python and I want to remove the common values from both lists. Potential solution:
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [43, 3123, 543, 76, 879, 32, 14241, 342, 2, 3, 4]
for i in x:
if i in y:
x.remove(i)
y.remove(i)
It seems correct, but it is not. The reason, in my opinion, is that by removing an item from the list, the index continues to iterate. Therefore, for two common values in lists where the values are next to each other, we will skip later values (the code will not go through it). The result will be:
>>> x
[1, 3, 5, 6, 8, 9, 10]
>>> y
[43, 3123, 543, 76, 879, 32, 14241, 342, 3]
So, we lack value '3'
.
Is the reason for this behavior the one I mentioned? or am I doing something else wrong?