CodeQL documentation

Duplicate key in dict literal

ID: py/duplicate-key-dict-literal
Kind: problem
Severity: warning
Precision: very-high
   - reliability
   - useless-code
   - external/cwe/cwe-561
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Dictionary literals are constructed in the order given in the source. This means that if a key is duplicated the second key-value pair will overwrite the first as a dictionary can only have one value per key.


Check for typos to ensure that the keys are supposed to be the same. If they are then decide which value is wanted and delete the other one.


This example will output “c” because the mapping between 2 and “b” is overwritten by the mapping from 2 to “c”. The programmer may have meant to map 3 to “c” instead.

dictionary = {1:"a", 2:"b", 2:"c"}
print dictionary[2]