This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Strings beginning with underscore not removed from lists - feature or bug?
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: steven.daprano, xiang.zhang, yemiteliyadu
Priority: normal Keywords:

Created on 2018-03-27 15:15 by yemiteliyadu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg314530 - (view) Author: (yemiteliyadu) Date: 2018-03-27 15:15
Strings beginning with underscore not removed from lists
Reproducible as shown below:

Python 3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test = ["a","_a","b","_b"]
>>> for i in test: print(i)
... 
a
_a
b
_b
>>> for i in test: test.remove(i)
... 
>>> test
['_a', '_b']
>>> 

Is this a feature or a bug?
A search through the docs did not show any mention of this.
msg314531 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2018-03-27 15:24
It's just the right behavior. You are modifying the list while iterating over it. It has no business of underscore.

>>> test = ['_a', 'a', '_b', 'b']
>>> for i in test: test.remove(i)
>>> test
['a', 'b']
msg314532 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2018-03-27 15:29
You may refer to stackoverflow for an explanation, for example, https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list
msg314533 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-03-27 15:33
In addition to Xiang Zhang's comments, this is neither a feature nor a bug, but a misunderstanding. This has nothing to do with strings or underscores:

py> L = [1, 2, 3, 4, 5]
py> for item in L:
...     L.remove(item)
...
py> L
[2, 4]


When you modify a list as you iterate over it, the results can be unexpected. Don't do it.

If you *must* modify a list that you are iterating over, you must do so backwards, so that you are only removing items from the end, not the beginning of the list:

py> L = [1, 2, 3, 4, 5]
py> for i in range(len(L)-1, -1, -1):
...     L.remove(L[i])
...
py> L
[]


But don't do that: it is nearly always must faster to make a copy of the list containing only the items you wish to keep, then assign back to the list using slicing. A list comprehension makes this an easy one-liner:

py> L = [1, 2, 3, 4, 5]
py> L[:] = [x for x in L if x > 4]
py> L
[5]
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77338
2018-03-27 15:33:44steven.dapranosetnosy: + steven.daprano
messages: + msg314533
2018-03-27 15:29:00xiang.zhangsetmessages: + msg314532
2018-03-27 15:24:21xiang.zhangsetstatus: open -> closed

nosy: + xiang.zhang
messages: + msg314531

resolution: not a bug
stage: resolved
2018-03-27 15:15:51yemiteliyaducreate