[PYTHON] Points to note when deleting multiple elements from the List

What happened

There are multiple elements to be deleted in the list, and when I deleted them while looping without thinking about anything, I found an element that matches the conditions but is not deleted.

Cause

It was so simple that I scanned it from the top of the list and deleted it, so the combination of index and element was wrong, and the element that was originally deleted remained.

Workaround

Changed to scan the list from reverse order

Before dealing

lst = [1, 2, 3]
for i in lst:
    if i in (1, 2):
        lst.remove(i)
print(lst)
#Execution result
[2, 3]

After dealing

lst = [1, 2, 3]
for I in reversed(lst):
    if i in (1, 2):
        lst.remove(i)
print(lst)
#Execution result
[3]

Commentary

If you delete it from the top of the list, the elements will be packed, but the index will remain the same. Therefore, the next deleted element is packed and not processed. Deleting the list from the back prevents the next element to be judged from being packed.

Verification

Before correspondence

>>> lst = [1, 2, 3]
>>> for i, l in enumerate(lst):
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...     if l in (1, 2):
...             lst.remove(l)
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...
index:0, value:1, list:[1, 2, 3]
index:0, value:1, list:[2, 3]
index:1, value:3, list:[2, 3]
index:1, value:3, list:[2, 3]

Since the value is 3 when the index is 1, 2, which should be removed by the judgment, is not processed. (If index is 1, value is 2 according to the original list) Do this in reverse order.

After correspondence

>>> lst = [1, 2, 3]
>>> for i, l in enumerate(reversed(lst)):
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...     if l in (1, 2):
...             lst.remove(l)
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...
index:0, value:3, list:[1, 2, 3]
index:0, value:3, list:[1, 2, 3]
index:1, value:2, list:[1, 2, 3]
index:1, value:2, list:[1, 3]
index:2, value:1, list:[1, 3]
index:2, value:1, list:[3]

Things to watch out for

Up to this point, when deleting with ** element **. One thing to be careful of is when deleting with ** index **. When deleting with ** index **, this writing method will result in an error.

>>> lst = [1, 2, 3]
>>> for i, l in enumerate(reversed(lst)):
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...     if l in (1, 2):
...             del lst[i]
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...
index:0, value:3, list:[1, 2, 3]
index:0, value:3, list:[1, 2, 3]
index:1, value:2, list:[1, 2, 3]
index:1, value:2, list:[1, 3]
index:2, value:1, list:[1, 3]
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
IndexError: list assignment index out of range

In this case, it can be dealt with by the following.

>>> lst = [1, 2, 3]
>>> for i, l in reversed(list(enumerate(lst))):
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...     if l in (1, 2):
...             del lst[i]
...     print("index:%s, value:%s, list:%s" % (i, l, lst))
...
index:2, value:3, list:[1, 2, 3]
index:2, value:3, list:[1, 2, 3]
index:1, value:2, list:[1, 2, 3]
index:1, value:2, list:[1, 3]
index:0, value:1, list:[1, 3]
index:0, value:1, list:[3]

Recommended Posts

Points to note when deleting multiple elements from the List
Points to note when switching from NAOqi OS 2.4.3 to 2.5.5
[Python] Solution to the problem that elements are linked when copying a list
Points to note when performing logistic regression with Statsmodels
Things to note when initializing a list in Python
Coloring points according to the distance from the regression curve
(Note) Points to be addicted to when installing Scilab on ArchLinux
Extract the value closest to a value from a Python list element
Points to note when making pandas read csv of excel output
Things to note when running Python on EC2 from AWS Lambda
When you want to sort a multidimensional list by multiple lines
In the python command python points to python3.8
Delete multiple elements in python list
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
Remove and retrieve arrays from fasta according to the ID list file
Python Note: When you want to know the attributes of an object
When running a Python shell from Electron, pass multiple arguments to run Python.
Points to consider when hitting SoftLayer API from an app on Bluemix
[Note] [PyTorch] From installation to easy usage
How to utilize multi-core from multiple languages
How to operate Linux from the console
Python amateurs try to summarize the list ①
Join List elements without''. (Retrieve String from list without'')
How to access the Datastore from the outside
DJango Note: From the beginning (form processing)
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary