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.

Author steven.daprano
Recipients kumarmakala, steven.daprano
Date 2021-02-07.03:22:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612668120.74.0.901132975099.issue43150@roundup.psfhosted.org>
In-reply-to
Content
The problem here is that you are shortening the list as you walk along it, which means you skip items. You expected to visit:

    "Emma", "Jon", "", "Kelly","Eric", "", "", "KXN", ""

in that order, but after visiting Emma, Jon and the first empty string, you delete the first empty string, which pushes every item down one position:

    "Emma", "Jon", "Kelly","Eric", "", "", "KXN", ""

The next item is now Eric, not Kelly, because Kelly is in the position already visited. So the *string* Kelly gets skipped. After visiting Eric and the next empty string, everything gets pushed down another spot:

    "Emma", "Jon", "Kelly","Eric", "", "KXN", ""

which moves the second empty string into the position that was already visited. The next item is now KXN, then the final empty string, which then deletes the *first remaining* empty string:

    "Emma", "Jon", "Kelly","Eric", "KXN", ""

and now you have reached the end of the string and there is nothing more to do.

You can see for yourself the items which are visited:


    >>> for item in str_list:
    ...     print('item =', repr(item))
    ...     if len(item) == 0:
    ...         str_list.remove(item)
    ... 
    item = 'Emma'
    item = 'Jon'
    item = ''
    item = 'Eric'
    item = ''
    item = 'KXN'
    item = ''


Here is perhaps a more clear way to see what you are doing wrong:

    >>> str_list = ['a', 'x', 'b', 'x', 'c', 'x', 'd', 'x', 'e', 'f']
    >>> for item in str_list:
    ...     print(item, ''.join(str_list))
    ...     if item == 'x':
    ...         str_list.remove(item)
    ... 
    a axbxcxdxef
    x axbxcxdxef
    x abxcxdxef
    x abcxdxef
    x abcdxef
    f abcdef


Every time you delete an item, the list shrinks, everything moves down one position, and the next item gets skipped because it has been moved into the slot already looked at.
History
Date User Action Args
2021-02-07 03:22:00steven.dapranosetrecipients: + steven.daprano, kumarmakala
2021-02-07 03:22:00steven.dapranosetmessageid: <1612668120.74.0.901132975099.issue43150@roundup.psfhosted.org>
2021-02-07 03:22:00steven.dapranolinkissue43150 messages
2021-02-07 03:22:00steven.dapranocreate