Skip to content

Missing Dict Items

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 refactoring id: missing-dict-items

Section titled “Sourcery refactoring id: missing-dict-items”

Add missing .items() call when unpacking a dictionary

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"}
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"}

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