[New-bugs-announce] [issue15214] list.startswith() and list.remove() fails to catch consecutive items in a list.

Isaac report at bugs.python.org
Thu Jun 28 05:44:53 CEST 2012


New submission from Isaac <isaacloves2run at yahoo.com>:

The simple repro below, shows that if a list of strings has two consecutive items that begin with the same letter, an iteration over the list to find and remove all strings that start with that letter fails.  The second string that starts with the same letter to remove remains in the list.

In the example below, both "bananna" and "blueberry" should be removed from the list, but only "bananna" is removed.

I verified this on both 2.7 and 3.2.

-----------------------------------------------------
--- Output ---
--------------
Before: ['apple', 'bananna', 'blueberry', 'coconut']
After:  ['apple', 'blueberry', 'coconut']

-----------------------------------------------------
--- Repro ---
-------------
itemList = ["apple", "bananna", "blueberry", "coconut"]
print("Before: {0}".format(itemList))

for item in itemList:
    if(item.startswith("b")):
        itemList.remove(item)

print("After:  {0}".format(itemList))

----------
files: listRemovalBug.py
messages: 164219
nosy: Eklutna
priority: normal
severity: normal
status: open
title: list.startswith() and list.remove() fails to catch consecutive items in a list.
type: behavior
versions: Python 2.7, Python 3.2
Added file: http://bugs.python.org/file26193/listRemovalBug.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue15214>
_______________________________________


More information about the New-bugs-announce mailing list