---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---
C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
 --> C420.py:6:5
  |
4 | def func():
5 |     numbers = [1, 2, 3]
6 |     {n: None for n in numbers}  # RUF025
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: Replace with `dict.fromkeys(iterable, value)`)
  |
5 |     numbers = [1, 2, 3]
  -     {n: None for n in numbers}  # RUF025
6 +     dict.fromkeys(numbers)  # RUF025
7 |
  |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:10:23
   |
 9 | def func():
10 |     for key, value in {n: 1 for n in [1, 2, 3]}.items():  # RUF025
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^
11 |         pass
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
9  | def func():
   -     for key, value in {n: 1 for n in [1, 2, 3]}.items():  # RUF025
10 +     for key, value in dict.fromkeys([1, 2, 3], 1).items():  # RUF025
11 |         pass
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:15:5
   |
14 | def func():
15 |     {n: 1.1 for n in [1, 2, 3]}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
14 | def func():
   -     {n: 1.1 for n in [1, 2, 3]}  # RUF025
15 +     dict.fromkeys([1, 2, 3], 1.1)  # RUF025
16 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:26:7
   |
24 |         return data
25 |
26 |     f({c: "a" for c in "12345"})  # RUF025
   |       ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
25 |
   -     f({c: "a" for c in "12345"})  # RUF025
26 +     f(dict.fromkeys("12345", "a"))  # RUF025
27 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:30:5
   |
29 | def func():
30 |     {n: True for n in [1, 2, 2]}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
29 | def func():
   -     {n: True for n in [1, 2, 2]}  # RUF025
30 +     dict.fromkeys([1, 2, 2], True)  # RUF025
31 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:34:5
   |
33 | def func():
34 |     {n: b"hello" for n in (1, 2, 2)}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
33 | def func():
   -     {n: b"hello" for n in (1, 2, 2)}  # RUF025
34 +     dict.fromkeys((1, 2, 2), b"hello")  # RUF025
35 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:38:5
   |
37 | def func():
38 |     {n: ... for n in [1, 2, 3]}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
37 | def func():
   -     {n: ... for n in [1, 2, 3]}  # RUF025
38 +     dict.fromkeys([1, 2, 3], ...)  # RUF025
39 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:42:5
   |
41 | def func():
42 |     {n: False for n in {1: "a", 2: "b"}}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
41 | def func():
   -     {n: False for n in {1: "a", 2: "b"}}  # RUF025
42 +     dict.fromkeys({1: "a", 2: "b"}, False)  # RUF025
43 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:46:5
   |
45 | def func():
46 |     {(a, b): 1 for (a, b) in [(1, 2), (3, 4)]}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
45 | def func():
   -     {(a, b): 1 for (a, b) in [(1, 2), (3, 4)]}  # RUF025
46 +     dict.fromkeys([(1, 2), (3, 4)], 1)  # RUF025
47 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:54:5
   |
53 |     a = f()
54 |     {n: a for n in [1, 2, 3]}  # RUF025
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
53 |     a = f()
   -     {n: a for n in [1, 2, 3]}  # RUF025
54 +     dict.fromkeys([1, 2, 3], a)  # RUF025
55 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
  --> C420.py:59:6
   |
57 | def func():
58 |     values = ["a", "b", "c"]
59 |     [{n: values for n in [1, 2, 3]}]  # RUF025
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Replace with `dict.fromkeys(iterable)`)
   |
58 |     values = ["a", "b", "c"]
   -     [{n: values for n in [1, 2, 3]}]  # RUF025
59 +     [dict.fromkeys([1, 2, 3], values)]  # RUF025
60 |
   |

C420 [*] Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
   --> C420.py:95:1
    |
 94 |   # https://github.com/astral-sh/ruff/issues/18764
 95 | / { # 1
 96 | | a # 2
 97 | | : # 3
 98 | | None # 4
 99 | | for # 5
100 | | a # 6
101 | | in # 7
102 | | iterable # 8
103 | | } # 9
    | |_^
    |
help: Replace with `dict.fromkeys(iterable, value)`)
   |
94 | # https://github.com/astral-sh/ruff/issues/18764
   - { # 1
   - a # 2
   - : # 3
   - None # 4
   - for # 5
   - a # 6
   - in # 7
   - iterable # 8
   - } # 9
95 + dict.fromkeys(iterable) # 9
   |
note: This is an unsafe fix and may change runtime behavior
