Skip to content

unnecessary-dict-comprehension-for-iterable (RUF025)#

Fix is sometimes available.

This rule is unstable and in preview. The --preview flag is required for use.

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.

Examples#

{a: None for a in iterable}
{a: 1 for a in iterable}

Use instead:

dict.fromkeys(iterable)
dict.fromkeys(iterable, 1)