seq1 = ['1', '2', '3']
seq2 = ['1', '2']
for elt1 in seq1:
for elt2 in seq2:
if elt1 == elt2:
seq1.remove(i)
print seq1
I want to leave seq1 = ['3']
but I find the result out seq1 = ['2', '3']
when I use this code.
I know how to solve it by changing this code. I just want to know why my code does not work. what is actually happening in the loop?
My analysis is:
The problem is number 2
in seq1
does not iterate .
Only '1'
, '3'
iterate.
So in the end, the result comes out ['2', '3']
(if it iterates on 2
in seq1
then the result will be ['3']
)