Skip to content

Remove Duplicate Dict Key

Sourcery suggestion id: remove-duplicate-dict-key


Remove duplicate keys when instantiating dicts.


my_dict = {a: 1, b: 2, a: 3, **d1, **d2, **d1}


my_dict = {b: 2, a: 3, **d2, **d1}


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: 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.