CodeQL documentation

Duplicate key in dict literal

ID: py/duplicate-key-dict-literal
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - 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.

Recommendation

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.

Example

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]

References

  • © GitHub, Inc.
  • Terms
  • Privacy