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.
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
Using update() improves readability and removes redundant method call overhead. In loops or performance-critical code, the difference
can become measurable.
Replace the for-loop with a single update() call. The method accepts any iterable.
my_set = {1, 2, 3}
other_collection = [4, 5, 6]
for element in other_collection:
my_set.add(element) # Noncompliant
my_set = {1, 2, 3}
other_collection = [4, 5, 6]
my_set.update(other_collection)
set.update() methodset,
frozenset