Skip to content

Remove Dict Items

Sourcery refactoring id: remove-dict-items

Description:

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

Before:

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

After:

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

Explanation:

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.

Back to top