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 tim.peters
Recipients docs@python, tim.peters
Date 2018-02-04.19:27:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1517772420.88.0.467229070634.issue32767@psf.upfronthosting.co.za>
In-reply-to
Content
This has come up repeatedly, and the docs should be updated to resolve it:

https://stackoverflow.com/questions/48603998/python-iterating-over-a-list-but-i-want-to-add-to-that-list-while-in-the-loop/48604036#48604036

Seemingly the only relevant documentation is in the reference manual, but it's flawed:

https://docs.python.org/3.7/reference/compound_stmts.html#the-for-statement

- The behavior it's describing is specific to list iterators, but it pretends to apply to "mutable sequences" in general (which may or may not mimic list iterators in relevant respects).

- It's not clear that the "length of the sequence" (list!) is evaluated anew on each iteration (not, e.g., captured once at the start of the `for` loop).

- While it describes things that can go wrong, it doesn't describe the common useful case:  appending to a list during iteration (for example, in a breadth-first search).
History
Date User Action Args
2018-02-04 19:27:00tim.peterssetrecipients: + tim.peters, docs@python
2018-02-04 19:27:00tim.peterssetmessageid: <1517772420.88.0.467229070634.issue32767@psf.upfronthosting.co.za>
2018-02-04 19:27:00tim.peterslinkissue32767 messages
2018-02-04 19:27:00tim.peterscreate