New submission from Tim Peters <t...@python.org>:

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).

----------
assignee: docs@python
components: Documentation
messages: 311614
nosy: docs@python, tim.peters
priority: normal
severity: normal
stage: needs patch
status: open
title: Mutating a list while iterating:  clarify the docs
type: enhancement
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32767>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to