Why should you not modify a list while iterating over it?
Common pitfalls and best practices
Audio flashcard · 0:14Nortren·
Why should you not modify a list while iterating over it?
0:14
Modifying a list during iteration produces unpredictable results because the iterator's internal index gets out of sync with the changing length. The safe approach is to iterate over a copy or to build a new list with the desired elements using a comprehension.
docs.python.org