35 lines
767 B
Plaintext
35 lines
767 B
Plaintext
== Why is this an issue?
|
|
|
|
A dictionary cannot have two values with the same key. When a key is repeated in a dictionary literal, only the last occurence will remain. Thus duplicate keys should be either modified or removed.
|
|
|
|
|
|
This rule raises an issue when the same value is used multiple times as a key in a dictionary literal.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
{"one": 1, "two": 2, "one": 3} # Noncompliant
|
|
|
|
def func(a1, a2, a3):
|
|
{a1: 1, a2: 2, a1: 3} # Noncompliant.
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
{"one": 1, "two": 2, "three": 3}
|
|
|
|
def func(a1, a2, a3):
|
|
{a1: 1, a2: 2, a3: 3}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.python.org/3/reference/expressions.html#dictionary-displays[Python documentation - Dictionary displays]
|
|
|