This rule raises an issue when a for-loop is used to add all elements from one collection to a set using the add() method.

Why is this an issue?

Adding elements to a set one at a time in a loop is unnecessary when the update() method exists:

for element in other_collection:
    my_set.add(element)

The update() method combines all additions into a single call and avoids the overhead of repeated add() invocations:

my_set.update(other_collection)  # Single call, more efficient

What is the potential impact?

Using update() improves readability and removes redundant method call overhead. In loops or performance-critical code, the difference can become measurable.

How to fix it

Replace the for-loop with a single update() call. The method accepts any iterable.

Code examples

Noncompliant code example

my_set = {1, 2, 3}
other_collection = [4, 5, 6]

for element in other_collection:
    my_set.add(element)  # Noncompliant

Compliant solution

my_set = {1, 2, 3}
other_collection = [4, 5, 6]

my_set.update(other_collection)

Resources

Documentation