Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings beginning with underscore not removed from lists - feature or bug? #77338

Closed
yemiteliyadu mannequin opened this issue Mar 27, 2018 · 4 comments
Closed

Strings beginning with underscore not removed from lists - feature or bug? #77338

yemiteliyadu mannequin opened this issue Mar 27, 2018 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@yemiteliyadu
Copy link
Mannequin

yemiteliyadu mannequin commented Mar 27, 2018

BPO 33157
Nosy @stevendaprano, @zhangyangyu

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-03-27.15:24:21.910>
created_at = <Date 2018-03-27.15:15:51.054>
labels = ['type-bug', 'invalid']
title = 'Strings beginning with underscore not removed from lists - feature or bug?'
updated_at = <Date 2018-03-27.15:33:44.520>
user = 'https://bugs.python.org/yemiteliyadu'

bugs.python.org fields:

activity = <Date 2018-03-27.15:33:44.520>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2018-03-27.15:24:21.910>
closer = 'xiang.zhang'
components = []
creation = <Date 2018-03-27.15:15:51.054>
creator = 'yemiteliyadu'
dependencies = []
files = []
hgrepos = []
issue_num = 33157
keywords = []
message_count = 4.0
messages = ['314530', '314531', '314532', '314533']
nosy_count = 3.0
nosy_names = ['steven.daprano', 'xiang.zhang', 'yemiteliyadu']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue33157'
versions = ['Python 3.6']

@yemiteliyadu
Copy link
Mannequin Author

yemiteliyadu mannequin commented Mar 27, 2018

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.

@yemiteliyadu yemiteliyadu mannequin added the type-bug An unexpected behavior, bug, or error label Mar 27, 2018
@zhangyangyu
Copy link
Member

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']

@zhangyangyu
Copy link
Member

You may refer to stackoverflow for an explanation, for example, https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list

@stevendaprano
Copy link
Member

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]

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants