By definition, a set cannot hold the same value multiple times. When instantiating a set literal with the same value repeated multiple times, only
the last occurrence of the duplicated value will remain.
Creating a set with redundant elements is prone to errors and confusion. A duplicated value in a set literal should be either:
- modified, as it was mistakenly put in the set instead an actual value which would lead to bugs and errors further in the program.
- removed, as it was a simple duplication, making the code confusing and difficult to maintain.
Code examples
Noncompliant code example
{"one", "two", "one"} # Noncompliant: the value "one" is duplicated.
def func(a1, a2, a3):
{a1, a2, a1} # Noncompliant: the value a1 is duplicated.
Compliant solution
{"one", "two", "three"}
def func(a1, a2, a3):
{a1, a2, a3}