xs[:] copies the list.
for x in xs- Iterate over the list xsas is.for x in xs[:]- Iterate over a copy of the list xs.
One of the reasons you want to "iterate over a copy" is to change the original list in place. Another common reason for βcopyingβ is the atomicity of the data you are dealing with. (i.e. another thread / process changes the structure of the list or data when you read it).
. , , - , .
:
xs = list(range(10))
for x in xs[:]:
if x % 2 == 0:
xs.remove(x)