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: list need to filter again because the continue empty str value?
Type: performance Stage: resolved
Components: Build Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: py_ok, steven.daprano
Priority: normal Keywords:

Created on 2021-08-19 03:56 by py_ok, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg399882 - (view) Author: (py_ok) Date: 2021-08-19 03:56
I`m poor in english.please run my code,Thanks.

def rv(list):
    for i in list:
        #print(type(i))
        #print(i.__len__())
        if (i.isspace() or i=='' or len(i)==0):
            list.remove(i)
    return list
list=['k', '', '', '', 'v', '', 'e', '', '', '', '73', '', 'p', '76', '']
print(rv(list))#['k', 'v', 'e', '73', '', 'p', '76', '']
The result still have empty str,I need to filter again.The reason is more empty val is linked.is a bug?or some reason?
msg399883 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-08-19 04:36
Not a bug. Use this instead:

def rv(items):
    for item in items[:]:  # iterate over a copy
        if not (item.isspace() or item == ''):
            items.remove(item)
    return items


Or the same as above, as a list comprehension, no need to make a copy:

items = [item for item in items if not (item.isspace() or item == '')]
msg399885 - (view) Author: (py_ok) Date: 2021-08-19 04:47
Thanks,Very timely resolve my problem
msg399886 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-08-19 04:53
To understand why your code doesn't work, run this:

items = ['a', '', 'b', '', 'c', '', 'd', '', 'e', '']

print(len(items))

for index, item in enumerate(items):
    print(index, repr(item), items)
    if item == '':
        items.remove('')


When you remove an item, all the remaining items slide over one position, into the slot that has already been inspected. That means that they get skipped.
msg399887 - (view) Author: (py_ok) Date: 2021-08-19 04:57
Yes,I remember that my error in Java,like you say it`s skiped

------------------ 原始邮件 ------------------
发件人: "Python tracker"<report@bugs.python.org&gt;; 
发送时间: 2021年8月19日(星期四) 中午12:53
收件人: "海"<1979239641@qq.com&gt;; 
主题: [issue44952] list need to filter again because the continue empty str value?

Steven D'Aprano <steve+python@pearwood.info&gt; added the comment:

To understand why your code doesn't work, run this:

items = ['a', '', 'b', '', 'c', '', 'd', '', 'e', '']

print(len(items))

for index, item in enumerate(items):
&nbsp;&nbsp;&nbsp; print(index, repr(item), items)
&nbsp;&nbsp;&nbsp; if item == '':
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.remove('')

When you remove an item, all the remaining items slide over one position, into the slot that has already been inspected. That means that they get skipped.

----------
resolution:&nbsp; -&gt; not a bug

_______________________________________
Python tracker <report@bugs.python.org&gt;
<" rel="noopener" target="_blank">https://bugs.python.org/issue44952&gt;;
_______________________________________
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89115
2021-08-19 04:57:47py_oksetmessages: + msg399887
2021-08-19 04:53:29steven.dapranosetresolution: not a bug
messages: + msg399886
2021-08-19 04:47:26py_oksetstatus: open -> closed

messages: + msg399885
stage: resolved
2021-08-19 04:36:06steven.dapranosetnosy: + steven.daprano
messages: + msg399883
2021-08-19 03:56:29py_okcreate