modified-iterating-set / E4703ΒΆ

Message emitted:

Iterated set '%s' is being modified inside for loop body, iterate through a copy of it instead.

Description:

Emitted when items are added or removed to a set being iterated through. Doing so raises a RuntimeError.

Problematic code:

fruits = {"apple", "orange", "mango"}
for fruit in fruits:
    fruits.add(fruit + "yum")  # [modified-iterating-set]

Correct code:

fruits = {"apple", "orange", "mango"}
for fruit in fruits.copy():
    fruits.add(fruit + "yum")

Created by the modified_iteration checker.