unnecessary-dict-comprehension-for-iterable (C420)
Derived from the flake8-comprehensions linter.
Fix is sometimes available.
What it does
Checks for unnecessary dict comprehension when creating a dictionary from an iterable.
Why is this bad?
It's unnecessary to use a dict comprehension to build a dictionary from an iterable when the value is static.
Prefer dict.fromkeys(iterable)
over {value: None for value in iterable}
,
as dict.fromkeys
is more readable and efficient.
Example
Use instead:
Fix safety
This rule's fix is marked as unsafe if there's comments inside the dict comprehension, as comments may be removed.
For example, the fix would be marked as unsafe in the following case: