---
source: crates/ruff_linter/src/rules/flake8_comprehensions/mod.rs
---
C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
 --> C404.py:1:1
  |
1 | dict([(i, i) for i in range(3)])
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 | dict([(i, i) for i in range(3)], z=4)
  |
help: Rewrite as a dict comprehension
  |
  - dict([(i, i) for i in range(3)])
1 + {i: i for i in range(3)}
2 | dict([(i, i) for i in range(3)], z=4)
  |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
 --> C404.py:7:4
  |
5 |     return x
6 |
7 | f'{dict([(s,s) for s in "ab"])}'
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
8 | f"{dict([(s,s) for s in 'ab'])}"
9 | f"{dict([(s, s) for s in 'ab'])}"
  |
help: Rewrite as a dict comprehension
  |
6 |
  - f'{dict([(s,s) for s in "ab"])}'
7 + f'{ {s: s for s in "ab"} }'
8 | f"{dict([(s,s) for s in 'ab'])}"
  |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:8:4
   |
 7 | f'{dict([(s,s) for s in "ab"])}'
 8 | f"{dict([(s,s) for s in 'ab'])}"
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 9 | f"{dict([(s, s) for s in 'ab'])}"
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
   |
help: Rewrite as a dict comprehension
  |
7 | f'{dict([(s,s) for s in "ab"])}'
  - f"{dict([(s,s) for s in 'ab'])}"
8 + f"{ {s: s for s in 'ab'} }"
9 | f"{dict([(s, s) for s in 'ab'])}"
  |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:9:4
   |
 7 | f'{dict([(s,s) for s in "ab"])}'
 8 | f"{dict([(s,s) for s in 'ab'])}"
 9 | f"{dict([(s, s) for s in 'ab'])}"
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
   |
help: Rewrite as a dict comprehension
   |
8  | f"{dict([(s,s) for s in 'ab'])}"
   - f"{dict([(s, s) for s in 'ab'])}"
9  + f"{ {s: s for s in 'ab'} }"
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:10:4
   |
 8 | f"{dict([(s,s) for s in 'ab'])}"
 9 | f"{dict([(s, s) for s in 'ab'])}"
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
   |
help: Rewrite as a dict comprehension
   |
9  | f"{dict([(s, s) for s in 'ab'])}"
   - f"{dict([(s,f(s)) for s in 'ab'])}"
10 + f"{ {s: f(s) for s in 'ab'} }"
11 |
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:12:4
   |
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
11 |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |
help: Rewrite as a dict comprehension
   |
11 |
   - f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
12 + f'{ {s: s for s in "ab"} | dict([(s,s) for s in "ab"])}'
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:12:34
   |
10 | f"{dict([(s,f(s)) for s in 'ab'])}"
11 |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
   |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |
help: Rewrite as a dict comprehension
   |
11 |
   - f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
12 + f'{dict([(s,s) for s in "ab"]) | {s: s for s in "ab"} }'
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:13:5
   |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 |
15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087
   |
help: Rewrite as a dict comprehension
   |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
   - f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
13 + f'{ {s: s for s in "ab"} | dict([(s,s) for s in "ab"]) }'
14 |
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:13:35
   |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
13 | f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 |
15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087
   |
help: Rewrite as a dict comprehension
   |
12 | f'{dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"])}'
   - f'{ dict([(s,s) for s in "ab"]) | dict([(s,s) for s in "ab"]) }'
13 + f'{ dict([(s,s) for s in "ab"]) | {s: s for s in "ab"} }'
14 |
   |
note: This is an unsafe fix and may change runtime behavior

C404 [*] Unnecessary list comprehension (rewrite as a dict comprehension)
  --> C404.py:16:14
   |
15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087
16 | saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]]))
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite as a dict comprehension
   |
15 | # Regression test for: https://github.com/astral-sh/ruff/issues/7087
   - saved.append(dict([(k, v)for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]]))
16 + saved.append({k: v for k,v in list(unique_instance.__dict__.items()) if k in [f.name for f in unique_instance._meta.fields]})
   |
note: This is an unsafe fix and may change runtime behavior
