[PYTHON] If you remove the list to be looped, you will get terrible.

Origin

One day when I was writing python code, something strange happened ...

x = [1, 2, 3, 4, 5]
for i in x:
    x.remove(i)
    print(x)

>>>
[2, 3, 4, 5]  #Recognize
[2, 4, 5]  # ???If you pull out 1 and 2[3, 4, 5]Then!?!?
[2, 4]  # ???????

By the way, it was the same when I tried it with ruby.

x = [1, 2, 3, 4, 5]
for i in x do
  x.delete(i)
  print(x)
end

>>>
[2, 3, 4, 5][2, 4, 5][2, 4] => [2, 4]

Hi, "for i in x" means

It seems that it is trying to refer to the 0, 1, 2, ..., nth of x every time the loop is looped.

Countermeasures

It is good to copy the list with [:] or copy ()

x = [1, 2, 3, 4, 5]
for i in x[:]:
    x.remove(i)
    print(x)

>>>
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]
from copy import copy

x = [1, 2, 3, 4, 5]
for i in copy(x):
    x.remove(i)
    print(x)

>>>
[2, 3, 4, 5]
[3, 4, 5]
[4, 5]
[5]
[]

Afterword

I was worried about 30 minutes, "Hmm, hmm ...". If you press it with numpy, you will be confused by the destructive methods of the standard list ...

Recommended Posts

If you remove the list to be looped, you will get terrible.
If you write the View decorator in urls.py in Django, the list will be higher.
What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
If you add sudo on ubuntu, it will be called the default python.
If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1
[Python] How to remove duplicate values from the list
If you get the error "basis matrix is singular to working precision" in GLPK
The programming language you want to be able to use
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
Try to get the function list of Python> os package
[Python] It might be useful to list the data frames
What to do if the package dependency cannot be repaired
What to do if you get "coverage unknown" in Coveralls
How to get the last (last) value in a list in Python
If you want your colleagues to use the same language
What to do if you get the message "" ~ .pkg "is corrupted and cannot be opened" when installing wxPython on Mac OS X
I tried to get the index of the list using the enumerate function
What to do if you get a minus zero in Python
If you give a list with the default argument of the function ...
What to do if you get a UnicodeDecodeError with pip install
What to do if you can't use the trash in Lubuntu 18.04.
Have python check if the string can be converted / converted to int
What to do when you get "I can't see the site !!!!"
How to find if you don't know the Java installation directory
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
If you just want to get the dump file of the server, it was convenient to build an http server
How to get the Python version
If you ride the Titanic ... die.
If you want to include awsebcli with CircleCI, specify the python version
[Linux] Command to get a list of commands executed in the past
I measured 6 methods to get the index of the maximum value (minimum value) of the list
What to do if you get lost in file reference with FileNotFoundError
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get stuck during Anaconda installation on Linux
If you want to get multiple statistics with groupby in pandas v1
What to do if you get a TypeError with numpy min, max
What to do if you get an error when trying to load mnist
Isn't Qt the strongest library if you want to easily output SVG?
Get the song name from the title of the video you tried to sing
What to do if you get Could not fetch URL 443 with pip
What to do if you get an error when installing Dlib (Ubuntu)
If you get a no attribute error in boto3, check the version