2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-06-30 12:50:28 +02:00
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.
2021-02-02 15:02:10 +01:00
2020-06-30 12:50:28 +02:00
This rule raises an issue when the same value is used multiple times as a key in a dictionary literal.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:50:28 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:50:28 +02:00
----
{"one": 1, "two": 2, "one": 3} # Noncompliant
def func(a1, a2, a3):
{a1: 1, a2: 2, a1: 3} # Noncompliant.
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:50:28 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:50:28 +02:00
----
{"one": 1, "two": 2, "three": 3}
def func(a1, a2, a3):
{a1: 1, a2: 2, a3: 3}
----
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:50:28 +02:00
* https://docs.python.org/3/reference/expressions.html#dictionary-displays[Python documentation - Dictionary displays]