---
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
  |
2 |     items = [1, 2, 3, 4]
  -     result = []
  -     for i in items:
  -         if i % 2:
  -             result.append(i)  # PERF401
3 +     result = [i for i in items if i % 2]  # PERF401
4 |
  |
note: This is an unsafe fix and may change runtime behavior

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
   |
10 |     items = [1, 2, 3, 4]
   -     result = []
   -     for i in items:
   -         result.append(i * i)  # PERF401
11 +     result = [i * i for i in items]  # PERF401
12 |
   |
note: This is an unsafe fix and may change runtime behavior

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
   |
78 |     items = [1, 2, 3, 4]
   -     result = []
   -     async for i in items:
   -         if i % 2:
   -             result.append(i)  # PERF401
79 +     result = [i async for i in items if i % 2]  # PERF401
80 |
   |
note: This is an unsafe fix and may change runtime behavior

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
   |
86 |     items = [1, 2, 3, 4]
   -     result = []
   -     async for i in items:
   -         result.append(i)  # PERF401
87 +     result = [i async for i in items]  # PERF401
88 |
   |
note: This is an unsafe fix and may change runtime behavior

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
   |
94 |     result = [1, 2]
   -     async for i in items:
   -         result.append(i)  # PERF401
95 +     result.extend([i async for i in items])  # PERF401
96 |
   |
note: This is an unsafe fix and may change runtime behavior

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
    |
100 |     result, _ = [1, 2, 3, 4], ...
    -     for i in range(10):
    -         result.append(i * 2)  # PERF401
101 +     result.extend(i * 2 for i in range(10))  # PERF401
102 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
107 |     if True:
    -         for i in range(10):  # single-line comment 1 should be protected
    -             # single-line comment 2 should be protected
    -             if i % 2:  # single-line comment 3 should be protected
    -                 result.append(i)  # PERF401
108 +         # single-line comment 1 should be protected
109 +         # single-line comment 2 should be protected
110 +         # single-line comment 3 should be protected
111 +         result.extend(i for i in range(10) if i % 2)  # PERF401
112 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
114 | def f():
    -     result = []  # comment after assignment should be protected
    -     for i in range(10):  # single-line comment 1 should be protected
    -         # single-line comment 2 should be protected
    -         if i % 2:  # single-line comment 3 should be protected
    -             result.append(i)  # PERF401
115 +     # comment after assignment should be protected
116 +     # single-line comment 1 should be protected
117 +     # single-line comment 2 should be protected
118 +     # single-line comment 3 should be protected
119 +     result = [i for i in range(10) if i % 2]  # PERF401
120 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
132 |     if param:
    -         new_layers = []
    -         for value in param:
    -             new_layers.append(value * 3)
133 +         new_layers = [value * 3 for value in param]
134 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
138 | def f():
    -     result = []
139 |     var = 1
    -     for _ in range(10):
    -         result.append(var + 1)  # PERF401
140 +     result = [var + 1 for _ in range(10)]  # PERF401
141 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
146 |     # make sure that `tmp` is not deleted
    -     tmp = 1; result = []  # comment should be protected
    -     for i in range(10):
    -         result.append(i + 1)  # PERF401
147 +     tmp = 1  # comment should be protected
148 +     result = [i + 1 for i in range(10)]  # PERF401
149 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
153 |     # make sure that `tmp` is not deleted
    -     result = []; tmp = 1  # comment should be protected
    -     for i in range(10):
    -         result.append(i + 1)  # PERF401
154 +     tmp = 1  # comment should be protected
155 +     result = [i + 1 for i in range(10)]  # PERF401
156 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
159 | def f():
    -     result = []  # comment should be protected
    -     for i in range(10):
    -         result.append(i * 2)  # PERF401
160 +     # comment should be protected
161 +     result = [i * 2 for i in range(10)]  # PERF401
162 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
167 |     result.append(1)
    -     for i in range(10):
    -         result.append(i * 2)  # PERF401
168 +     result.extend(i * 2 for i in range(10))  # PERF401
169 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
186 | def f():
    -     result = []
    -     for val in range(5):
    -         result.append(val * 2)  # PERF401
187 +     result = [val * 2 for val in range(5)]  # PERF401
188 |     val = 1
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
195 |     i = [1, 2, 3]
    -     result = []
    -     for i in i:
    -         result.append(i + 1)  # PERF401
196 +     result = [i + 1 for i in i]  # PERF401
197 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
201 | def f():
    -     result = []
    -     for i in range(  # Comment 1 should not be duplicated
202 +     # Comment 3
203 +     # Comment 4
204 +     result = [(
205 +                     i + 1,
206 +                     # Comment 5
207 +                     2,
208 +                 ) for i in range(  # Comment 1 should not be duplicated
209 |         (
210 |             2  # Comment 2
211 |             + 1
212 |         )
    -     ):  # Comment 3
    -         if i % 2:  # Comment 4
    -             result.append(
    -                 (
    -                     i + 1,
    -                     # Comment 5
    -                     2,
    -                 )
    -             )  # PERF401
213 +     ) if i % 2]  # PERF401
214 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
219 | def f():
    -     result: list[int] = []
    -     for i in range(10):
    -         result.append(i * 2)  # PERF401
220 +     result: list[int] = [i * 2 for i in range(10)]  # PERF401
221 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
226 |     a, b = [1, 2, 3], [4, 5, 6]
    -     result = []
    -     for i in a, b:
    -         result.append(i[0] + i[1])  # PERF401
227 +     result = [i[0] + i[1] for i in (a, b)]  # PERF401
228 |     return result
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
234 |     values = [1, 2, 3]
    -     result = []
235 |     for a in values:
236 |         print(a)
    -     for a in values:
    -         result.append(a + 1)  # PERF401
237 +     result = [a + 1 for a in values]  # PERF401
238 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
243 |     def g():
    -         for a in values:
    -             result.append(a + 1)  # PERF401
244 +         result.extend(a + 1 for a in values)  # PERF401
245 |     result = []
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
257 | def f():
    -     items = []
258 |
    -     for i in range(5):
    -         if j := i:
    -             items.append(j)
259 +     items = [j for i in range(5) if (j := i)]
260 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
265 |     values = [1, 2, 3]
    -     result = list()  # this should be replaced with a comprehension
    -     for i in values:
    -         result.append(i + 1)  # PERF401
266 +     # this should be replaced with a comprehension
267 +     result = [i + 1 for i in values]  # PERF401
268 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
271 |     src = [1]
    -     dst = []
272 |
    -     for i in src:
    -         if True if True else False:
    -             dst.append(i)
273 +     dst = [i for i in src if (True if True else False)]
274 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
277 |
    -     for i in src:
    -         if lambda: 0:
    -             dst.append(i)
278 +     dst.extend(i for i in src if (lambda: 0))
279 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
283 |     i = "xyz"
    -     result = []
    -     for i in range(3):
    -         result.append(x for x in [i])
284 +     result = [(x for x in [i]) for i in range(3)]
285 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
289 |     i = "xyz"
    -     result = []
    -     for i in range(3):
    -         result.append((x for x in [i]))
290 +     result = [(x for x in [i]) for i in range(3)]
291 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
311 |     original = list(range(10000))
    -     filtered = []
    -     for i in original:
    -         if (
    -             i
    -             # comment
    -         ):
    -             filtered.append(i)
312 +     # comment
313 +     filtered = [i for i in original if i]
314 |
    |
note: This is an unsafe fix and may change runtime behavior

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
    |
322 |     original = list(range(10000))
    -     filtered = []
    -     for (
    -         i  # comment
    -     ) in original:
    -         if i > 0:
    -             filtered.append(i)
323 +     # comment
324 +     filtered = [i for i in original if i > 0]
    |
note: This is an unsafe fix and may change runtime behavior
