---
source: crates/ruff_linter/src/rules/perflint/mod.rs
---
PERF401 Use a list comprehension to create a transformed list
 --> PERF401.py:6:13
  |
4 |     for i in items:
5 |         if i % 2:
6 |             result.append(i)  # PERF401
  |             ^^^^^^^^^^^^^^^^
  |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
  --> PERF401.py:13:9
   |
11 |     result = []
12 |     for i in items:
13 |         result.append(i * i)  # PERF401
   |         ^^^^^^^^^^^^^^^^^^^^
   |
help: Replace for loop with list comprehension

PERF401 Use an async list comprehension to create a transformed list
  --> PERF401.py:82:13
   |
80 |     async for i in items:
81 |         if i % 2:
82 |             result.append(i)  # PERF401
   |             ^^^^^^^^^^^^^^^^
   |
help: Replace for loop with list comprehension

PERF401 Use an async list comprehension to create a transformed list
  --> PERF401.py:89:9
   |
87 |     result = []
88 |     async for i in items:
89 |         result.append(i)  # PERF401
   |         ^^^^^^^^^^^^^^^^
   |
help: Replace for loop with list comprehension

PERF401 Use `list.extend` with an async comprehension to create a transformed list
  --> PERF401.py:96:9
   |
94 |     result = [1, 2]
95 |     async for i in items:
96 |         result.append(i)  # PERF401
   |         ^^^^^^^^^^^^^^^^
   |
help: Replace for loop with list.extend

PERF401 Use `list.extend` to create a transformed list
   --> PERF401.py:102:9
    |
100 |     result, _ = [1, 2, 3, 4], ...
101 |     for i in range(10):
102 |         result.append(i * 2)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list.extend

PERF401 Use `list.extend` to create a transformed list
   --> PERF401.py:111:17
    |
109 |             # single-line comment 2 should be protected
110 |             if i % 2:  # single-line comment 3 should be protected
111 |                 result.append(i)  # PERF401
    |                 ^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list.extend

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:119:13
    |
117 |         # single-line comment 2 should be protected
118 |         if i % 2:  # single-line comment 3 should be protected
119 |             result.append(i)  # PERF401
    |             ^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:135:13
    |
133 |         new_layers = []
134 |         for value in param:
135 |             new_layers.append(value * 3)
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:142:9
    |
140 |     var = 1
141 |     for _ in range(10):
142 |         result.append(var + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:149:9
    |
147 |     tmp = 1; result = []  # comment should be protected
148 |     for i in range(10):
149 |         result.append(i + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:156:9
    |
154 |     result = []; tmp = 1  # comment should be protected
155 |     for i in range(10):
156 |         result.append(i + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:162:9
    |
160 |     result = []  # comment should be protected
161 |     for i in range(10):
162 |         result.append(i * 2)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use `list.extend` to create a transformed list
   --> PERF401.py:169:9
    |
167 |     result.append(1)
168 |     for i in range(10):
169 |         result.append(i * 2)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list.extend

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:189:9
    |
187 |     result = []
188 |     for val in range(5):
189 |         result.append(val * 2)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^^^
190 |     val = 1
191 |     print(val)
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:198:9
    |
196 |     result = []
197 |     for i in i:
198 |         result.append(i + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:210:13
    |
208 |       ):  # Comment 3
209 |           if i % 2:  # Comment 4
210 | /             result.append(
211 | |                 (
212 | |                     i + 1,
213 | |                     # Comment 5
214 | |                     2,
215 | |                 )
216 | |             )  # PERF401
    | |_____________^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:222:9
    |
220 |     result: list[int] = []
221 |     for i in range(10):
222 |         result.append(i * 2)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:229:9
    |
227 |     result = []
228 |     for i in a, b:
229 |         result.append(i[0] + i[1])  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
230 |     return result
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:239:9
    |
237 |         print(a)
238 |     for a in values:
239 |         result.append(a + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
240 |
241 | def f():
    |
help: Replace for loop with list comprehension

PERF401 Use `list.extend` to create a transformed list
   --> PERF401.py:245:13
    |
243 |     def g():
244 |         for a in values:
245 |             result.append(a + 1)  # PERF401
    |             ^^^^^^^^^^^^^^^^^^^^
246 |     result = []
    |
help: Replace for loop with list.extend

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:262:13
    |
260 |     for i in range(5):
261 |         if j := i:
262 |             items.append(j)
    |             ^^^^^^^^^^^^^^^
263 |
264 | def f():
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:268:9
    |
266 |     result = list()  # this should be replaced with a comprehension
267 |     for i in values:
268 |         result.append(i + 1)  # PERF401
    |         ^^^^^^^^^^^^^^^^^^^^
269 |
270 | def f():
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:276:13
    |
274 |     for i in src:
275 |         if True if True else False:
276 |             dst.append(i)
    |             ^^^^^^^^^^^^^
277 |
278 |     for i in src:
    |
help: Replace for loop with list comprehension

PERF401 Use `list.extend` to create a transformed list
   --> PERF401.py:280:13
    |
278 |     for i in src:
279 |         if lambda: 0:
280 |             dst.append(i)
    |             ^^^^^^^^^^^^^
281 |
282 | def f():
    |
help: Replace for loop with list.extend

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:286:9
    |
284 |     result = []
285 |     for i in range(3):
286 |         result.append(x for x in [i])
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
287 |
288 | def f():
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:292:9
    |
290 |     result = []
291 |     for i in range(3):
292 |         result.append((x for x in [i]))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
293 |
294 | G_INDEX = None
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:318:13
    |
316 |             # comment
317 |         ):
318 |             filtered.append(i)
    |             ^^^^^^^^^^^^^^^^^^
319 |
320 | def f():
    |
help: Replace for loop with list comprehension

PERF401 Use a list comprehension to create a transformed list
   --> PERF401.py:328:13
    |
326 |     ) in original:
327 |         if i > 0:
328 |             filtered.append(i)
    |             ^^^^^^^^^^^^^^^^^^
    |
help: Replace for loop with list comprehension
