[issue41454] while loop bug on list

Dexter Ramos report at bugs.python.org
Sat Aug 1 10:07:41 EDT 2020


Dexter Ramos <blitzdex27 at gmail.com> added the comment:

Thank you Mr. Erick Smith. Now I know. I also tried to find the hard way like this:

--------finding nemo-------------
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4] --->index
[4, 1, 2, 5, 7, 4, 2, 8, 9, 5, 3, 2, 4, 6] --->original list
 /                                         --->started at index 0 with value of 4
[1, 2, 5, 7, 2, 8, 9, 5, 3, 2, 6, 4] --->1st iteration, all 4's are removed then appended so the index adjusted
    /                                --->next at index 1 with a value of 2 (whereas value 1 was skipped which had index 1 originally; this is not seen on the output because it has no duplicate)
[1, 5, 7, 8, 9, 5, 3, 6, 4, 2] --->3rd iteration
       /                             --->next at index 2 with value of 7; value 5 was skipped which had the index 2 originally; cause found!
[1, 5, 8, 9, 5, 3, 6, 4, 2, 7] --->4th ...
...
---------------nemo found-----------------------
Credits to you.

Here is the new working code:
-------code------------------------------------------------
bunchofnumbers = [4, 1, 2, 5, 7, 4, 2, 8, 9, 5, 3, 2, 4, 6]
for eachnumber in bunchofnumbers.copy():
    while eachnumber in bunchofnumbers:
        bunchofnumbers.remove(eachnumber)
    bunchofnumbers.append(eachnumber)
bunchofnumbers.sort()
-------end of code-----------------------------------------
OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
print(bunchofnumbers)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41454>
_______________________________________


More information about the Python-bugs-list mailing list