[docs] [issue32767] Mutating a list while iterating: clarify the docs
Tim Peters
report at bugs.python.org
Sun Feb 4 14:27:00 EST 2018
New submission from Tim Peters <tim at 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 at python
components: Documentation
messages: 311614
nosy: docs at 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 <report at bugs.python.org>
<https://bugs.python.org/issue32767>
_______________________________________
More information about the docs
mailing list