.. _modified-iterating-list:

modified-iterating-list / W4701
===============================

**Message emitted:**

Iterated list '%s' is being modified inside for loop body, consider iterating through a copy of it instead.

**Description:**

*Emitted when items are added or removed to a list being iterated through. Doing so can result in unexpected behaviour, that is why it is preferred to use a copy of the list.*

**Problematic code:**

.. literalinclude:: /data/messages/m/modified-iterating-list/bad.py
   :language: python

**Correct code:**

.. literalinclude:: /data/messages/m/modified-iterating-list/good.py
   :language: python



Created by the `modified_iteration <https://github.com/PyCQA/pylint/blob/main/pylint/checkers/modified_iterating_checker.py>`__ checker.