Remove Duplicate Dict Key
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery suggestion id: remove-duplicate-dict-key
Section titled “Sourcery suggestion id: remove-duplicate-dict-key”Description:
Section titled “Description:”Remove duplicate keys when instantiating dicts.
Before:
Section titled “Before:”my_dict = {a: 1, b: 2, a: 3, **d1, **d2, **d1}After:
Section titled “After:”my_dict = {b: 2, a: 3, **d2, **d1}Explanation:
Section titled “Explanation:”Dictionary keys must be unique. Hence, repeated keys are redundant and can be removed from its inialization to increase the conciseness and clarity of the code.
!!! note “Note”
This is a breaking change in Python 3.7+ since dict keys are kept in their
insertion order. By applying this suggestion, the order of insertion is
changed. If your application does not rely on this ordering, then there won’t be
any changes in behaviour.