A nesting problem of for and if in python

the code is as follows:

s = ["adam", "xxx", "lisa"]

L = ["adam", "xxx", "lisa", "bart"]

for x in L:

print "this is ---%s" % x
if x in s:
    print x
    L.remove(x)


print L

the results are as follows:
this is-adam
adam
this is-lisa
lisa

["xxx"," bart"]
[Finished in 0.2s]

Why is" xxx" directly ignored?

Sep.10,2021

the program runs as follows

Loop 1 br x points to adam: in L
print this is-adam
print adam
removes the adam,x in L pointing to xxx in L
cycle is over, x continues to go down, pointing to lisa in L

Loop 2 lisa
print this is-lisa
print lisa
remove the lisa,x in L pointing to bart in L
cycle is over, x continues to go down, can't go down, the whole cycle ends


is not ignored, but the position has changed


Let's see if you can understand it ( python3 ):
s = ["adam", "xxx", "lisa"]
L = ["adam", "xxx", "lisa", "bart"]
for x in L[:]:
    print("this is ---%s" % x)
    if x in s:
        print(x)
        L.remove(x)
print(L)
output
this is ---adam
adam
this is ---xxx
xxx
this is ---lisa
lisa
this is ---bart
['bart']
Menu