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: while loop bug on list
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dexter Ramos, eric.smith, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2020-08-01 13:16 by Dexter Ramos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
app.py Dexter Ramos, 2020-08-01 13:16 just file with for, while and list
Messages (3)
msg374661 - (view) Author: Dexter Ramos (Dexter Ramos) Date: 2020-08-01 13:16
The output should have no duplicates but it has. When I added another "5" on the list it goes okay and is still okay if I add more. But when there is only two 5's it won't do its job.
msg374663 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-08-01 13:45
You're mutating the list while iterating over it, which is causing the behavior you're seeing. This isn't a bug.

See for example https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list

Also, when reporting bugs in the future, please include the output you got, and how it differs from what you expected. It's difficult to guess.
msg374664 - (view) Author: Dexter Ramos (Dexter Ramos) Date: 2020-08-01 14:07
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)
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85626
2020-08-01 14:07:40Dexter Ramossetmessages: + msg374664
2020-08-01 13:45:17eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg374663

resolution: not a bug
stage: resolved
2020-08-01 13:16:15Dexter Ramoscreate