Skip to content

Missing Dict Items

Sourcery refactoring id: missing-dict-items

Description

Add missing .items() call when unpacking a dictionary

Before

data = {"a": 1, "b": 2}
for key, value in data:
    process(key, value)
data = {"x": 1, "y": 2, "z": 3}
filtered = {k: v for k, v in data if k == "x"}

After

data = {"a": 1, "b": 2}
for key, value in data.items():
    process(key, value)
data = {"x": 1, "y": 2, "z": 3}
filtered = {k: v for k, v in data.items() if k == "x"}

Explanation

When writing a loop to unpack a dictionary it is easy to forget to call .items(). This suggestion adds in the missing call.

Back to top