Skip to content

incorrect-dict-iterator (PERF102)#

Derived from the Perflint linter.

Fix is always available.

What it does#

Checks for uses of dict.items() that discard either the key or the value when iterating over the dictionary.

Why is this bad?#

If you only need the keys or values of a dictionary, you should use dict.keys() or dict.values() respectively, instead of dict.items(). These specialized methods are more efficient than dict.items(), as they avoid allocating tuples for every item in the dictionary. They also communicate the intent of the code more clearly.

Note that, as with all perflint rules, this is only intended as a micro-optimization, and will have a negligible impact on performance in most cases.


obj = {"a": 1, "b": 2}
for key, value in obj.items():

Use instead:

obj = {"a": 1, "b": 2}
for value in obj.values():