Skip to content

Remove Dict Items

Sourcery refactoring id: remove-dict-items


Remove unnecessary calls to dict.items when the values are not used.


for name, age in people.items():
    print("Hi, my name is", name)


for name in people:
    print("Hi, my name is", name)


Calling dict.items is only necessary when both the dictionary keys and values are used. When the values are not used, it is sufficient to iterate over the dictionary keys only.

The code is now easier to read, avoiding an unnecessary call to dict.items, and removing an unused variable - the dictionary values.