---
source: crates/ruff_linter/src/rules/ruff/mod.rs
snapshot_kind: text
---
RUF015.py:4:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
  |
3 | # RUF015
4 | list(x)[0]
  | ^^^^^^^^^^ RUF015
5 | tuple(x)[0]
6 | list(i for i in x)[0]
  |
  = help: Replace with `next(iter(x))`

ℹ Unsafe fix
1 1 | x = range(10)
2 2 | 
3 3 | # RUF015
4   |-list(x)[0]
  4 |+next(iter(x))
5 5 | tuple(x)[0]
6 6 | list(i for i in x)[0]
7 7 | [i for i in x][0]

RUF015.py:5:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
  |
3 | # RUF015
4 | list(x)[0]
5 | tuple(x)[0]
  | ^^^^^^^^^^^ RUF015
6 | list(i for i in x)[0]
7 | [i for i in x][0]
  |
  = help: Replace with `next(iter(x))`

ℹ Unsafe fix
2 2 | 
3 3 | # RUF015
4 4 | list(x)[0]
5   |-tuple(x)[0]
  5 |+next(iter(x))
6 6 | list(i for i in x)[0]
7 7 | [i for i in x][0]
8 8 | 

RUF015.py:6:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
  |
4 | list(x)[0]
5 | tuple(x)[0]
6 | list(i for i in x)[0]
  | ^^^^^^^^^^^^^^^^^^^^^ RUF015
7 | [i for i in x][0]
  |
  = help: Replace with `next(iter(x))`

ℹ Unsafe fix
3 3 | # RUF015
4 4 | list(x)[0]
5 5 | tuple(x)[0]
6   |-list(i for i in x)[0]
  6 |+next(iter(x))
7 7 | [i for i in x][0]
8 8 | 
9 9 | # OK (not indexing (solely) the first element)

RUF015.py:7:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
  |
5 | tuple(x)[0]
6 | list(i for i in x)[0]
7 | [i for i in x][0]
  | ^^^^^^^^^^^^^^^^^ RUF015
8 | 
9 | # OK (not indexing (solely) the first element)
  |
  = help: Replace with `next(iter(x))`

ℹ Unsafe fix
4 4 | list(x)[0]
5 5 | tuple(x)[0]
6 6 | list(i for i in x)[0]
7   |-[i for i in x][0]
  7 |+next(iter(x))
8 8 | 
9 9 | # OK (not indexing (solely) the first element)
10 10 | list(x)

RUF015.py:29:1: RUF015 [*] Prefer `next(i + 1 for i in x)` over single element slice
   |
28 | # RUF015 (doesn't mirror the underlying list)
29 | [i + 1 for i in x][0]
   | ^^^^^^^^^^^^^^^^^^^^^ RUF015
30 | [i for i in x if i > 5][0]
31 | [(i, i + 1) for i in x][0]
   |
   = help: Replace with `next(i + 1 for i in x)`

ℹ Unsafe fix
26 26 | [i for i in x][::]
27 27 | 
28 28 | # RUF015 (doesn't mirror the underlying list)
29    |-[i + 1 for i in x][0]
   29 |+next(i + 1 for i in x)
30 30 | [i for i in x if i > 5][0]
31 31 | [(i, i + 1) for i in x][0]
32 32 | 

RUF015.py:30:1: RUF015 [*] Prefer `next(i for i in x if i > 5)` over single element slice
   |
28 | # RUF015 (doesn't mirror the underlying list)
29 | [i + 1 for i in x][0]
30 | [i for i in x if i > 5][0]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF015
31 | [(i, i + 1) for i in x][0]
   |
   = help: Replace with `next(i for i in x if i > 5)`

ℹ Unsafe fix
27 27 | 
28 28 | # RUF015 (doesn't mirror the underlying list)
29 29 | [i + 1 for i in x][0]
30    |-[i for i in x if i > 5][0]
   30 |+next(i for i in x if i > 5)
31 31 | [(i, i + 1) for i in x][0]
32 32 | 
33 33 | # RUF015 (multiple generators)

RUF015.py:31:1: RUF015 [*] Prefer `next((i, i + 1) for i in x)` over single element slice
   |
29 | [i + 1 for i in x][0]
30 | [i for i in x if i > 5][0]
31 | [(i, i + 1) for i in x][0]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF015
32 | 
33 | # RUF015 (multiple generators)
   |
   = help: Replace with `next((i, i + 1) for i in x)`

ℹ Unsafe fix
28 28 | # RUF015 (doesn't mirror the underlying list)
29 29 | [i + 1 for i in x][0]
30 30 | [i for i in x if i > 5][0]
31    |-[(i, i + 1) for i in x][0]
   31 |+next((i, i + 1) for i in x)
32 32 | 
33 33 | # RUF015 (multiple generators)
34 34 | y = range(10)

RUF015.py:35:1: RUF015 [*] Prefer `next(i + j for i in x for j in y)` over single element slice
   |
33 | # RUF015 (multiple generators)
34 | y = range(10)
35 | [i + j for i in x for j in y][0]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF015
36 | 
37 | # RUF015
   |
   = help: Replace with `next(i + j for i in x for j in y)`

ℹ Unsafe fix
32 32 | 
33 33 | # RUF015 (multiple generators)
34 34 | y = range(10)
35    |-[i + j for i in x for j in y][0]
   35 |+next(i + j for i in x for j in y)
36 36 | 
37 37 | # RUF015
38 38 | list(range(10))[0]

RUF015.py:38:1: RUF015 [*] Prefer `next(iter(range(10)))` over single element slice
   |
37 | # RUF015
38 | list(range(10))[0]
   | ^^^^^^^^^^^^^^^^^^ RUF015
39 | list(x.y)[0]
40 | list(x["y"])[0]
   |
   = help: Replace with `next(iter(range(10)))`

ℹ Unsafe fix
35 35 | [i + j for i in x for j in y][0]
36 36 | 
37 37 | # RUF015
38    |-list(range(10))[0]
   38 |+next(iter(range(10)))
39 39 | list(x.y)[0]
40 40 | list(x["y"])[0]
41 41 | [*range(10)][0]

RUF015.py:39:1: RUF015 [*] Prefer `next(iter(x.y))` over single element slice
   |
37 | # RUF015
38 | list(range(10))[0]
39 | list(x.y)[0]
   | ^^^^^^^^^^^^ RUF015
40 | list(x["y"])[0]
41 | [*range(10)][0]
   |
   = help: Replace with `next(iter(x.y))`

ℹ Unsafe fix
36 36 | 
37 37 | # RUF015
38 38 | list(range(10))[0]
39    |-list(x.y)[0]
   39 |+next(iter(x.y))
40 40 | list(x["y"])[0]
41 41 | [*range(10)][0]
42 42 | [*x["y"]][0]

RUF015.py:40:1: RUF015 [*] Prefer `next(iter(x["y"]))` over single element slice
   |
38 | list(range(10))[0]
39 | list(x.y)[0]
40 | list(x["y"])[0]
   | ^^^^^^^^^^^^^^^ RUF015
41 | [*range(10)][0]
42 | [*x["y"]][0]
   |
   = help: Replace with `next(iter(x["y"]))`

ℹ Unsafe fix
37 37 | # RUF015
38 38 | list(range(10))[0]
39 39 | list(x.y)[0]
40    |-list(x["y"])[0]
   40 |+next(iter(x["y"]))
41 41 | [*range(10)][0]
42 42 | [*x["y"]][0]
43 43 | [*x.y][0]

RUF015.py:41:1: RUF015 [*] Prefer `next(iter(range(10)))` over single element slice
   |
39 | list(x.y)[0]
40 | list(x["y"])[0]
41 | [*range(10)][0]
   | ^^^^^^^^^^^^^^^ RUF015
42 | [*x["y"]][0]
43 | [*x.y][0]
   |
   = help: Replace with `next(iter(range(10)))`

ℹ Unsafe fix
38 38 | list(range(10))[0]
39 39 | list(x.y)[0]
40 40 | list(x["y"])[0]
41    |-[*range(10)][0]
   41 |+next(iter(range(10)))
42 42 | [*x["y"]][0]
43 43 | [*x.y][0]
44 44 | [* x.y][0]

RUF015.py:42:1: RUF015 [*] Prefer `next(iter(x["y"]))` over single element slice
   |
40 | list(x["y"])[0]
41 | [*range(10)][0]
42 | [*x["y"]][0]
   | ^^^^^^^^^^^^ RUF015
43 | [*x.y][0]
44 | [* x.y][0]
   |
   = help: Replace with `next(iter(x["y"]))`

ℹ Unsafe fix
39 39 | list(x.y)[0]
40 40 | list(x["y"])[0]
41 41 | [*range(10)][0]
42    |-[*x["y"]][0]
   42 |+next(iter(x["y"]))
43 43 | [*x.y][0]
44 44 | [* x.y][0]
45 45 | [

RUF015.py:43:1: RUF015 [*] Prefer `next(iter(x.y))` over single element slice
   |
41 | [*range(10)][0]
42 | [*x["y"]][0]
43 | [*x.y][0]
   | ^^^^^^^^^ RUF015
44 | [* x.y][0]
45 | [
   |
   = help: Replace with `next(iter(x.y))`

ℹ Unsafe fix
40 40 | list(x["y"])[0]
41 41 | [*range(10)][0]
42 42 | [*x["y"]][0]
43    |-[*x.y][0]
   43 |+next(iter(x.y))
44 44 | [* x.y][0]
45 45 | [
46 46 |     *x.y

RUF015.py:44:1: RUF015 [*] Prefer `next(iter(x.y))` over single element slice
   |
42 | [*x["y"]][0]
43 | [*x.y][0]
44 | [* x.y][0]
   | ^^^^^^^^^^ RUF015
45 | [
46 |     *x.y
   |
   = help: Replace with `next(iter(x.y))`

ℹ Unsafe fix
41 41 | [*range(10)][0]
42 42 | [*x["y"]][0]
43 43 | [*x.y][0]
44    |-[* x.y][0]
   44 |+next(iter(x.y))
45 45 | [
46 46 |     *x.y
47 47 | ][0]

RUF015.py:45:1: RUF015 [*] Prefer `next(iter(x.y))` over single element slice
   |
43 |   [*x.y][0]
44 |   [* x.y][0]
45 | / [
46 | |     *x.y
47 | | ][0]
   | |____^ RUF015
48 |   
49 |   # RUF015 (multi-line)
   |
   = help: Replace with `next(iter(x.y))`

ℹ Unsafe fix
42 42 | [*x["y"]][0]
43 43 | [*x.y][0]
44 44 | [* x.y][0]
45    |-[
46    |-    *x.y
47    |-][0]
   45 |+next(iter(x.y))
48 46 | 
49 47 | # RUF015 (multi-line)
50 48 | revision_heads_map_ast = [

RUF015.py:50:26: RUF015 [*] Prefer `next(...)` over single element slice
   |
49 |   # RUF015 (multi-line)
50 |   revision_heads_map_ast = [
   |  __________________________^
51 | |     a
52 | |     for a in revision_heads_map_ast_obj.body
53 | |     if isinstance(a, ast.Assign) and a.targets[0].id == "REVISION_HEADS_MAP"
54 | | ][0]
   | |____^ RUF015
55 |   
56 |   # RUF015 (zip)
   |
   = help: Replace with `next(...)`

ℹ Unsafe fix
47 47 | ][0]
48 48 | 
49 49 | # RUF015 (multi-line)
50    |-revision_heads_map_ast = [
   50 |+revision_heads_map_ast = next(
51 51 |     a
52 52 |     for a in revision_heads_map_ast_obj.body
53 53 |     if isinstance(a, ast.Assign) and a.targets[0].id == "REVISION_HEADS_MAP"
54    |-][0]
   54 |+)
55 55 | 
56 56 | # RUF015 (zip)
57 57 | list(zip(x, y))[0]

RUF015.py:57:1: RUF015 [*] Prefer `next(zip(x, y))` over single element slice
   |
56 | # RUF015 (zip)
57 | list(zip(x, y))[0]
   | ^^^^^^^^^^^^^^^^^^ RUF015
58 | [*zip(x, y)][0]
   |
   = help: Replace with `next(zip(x, y))`

ℹ Unsafe fix
54 54 | ][0]
55 55 | 
56 56 | # RUF015 (zip)
57    |-list(zip(x, y))[0]
   57 |+next(zip(x, y))
58 58 | [*zip(x, y)][0]
59 59 | 
60 60 | # RUF015 (pop)

RUF015.py:58:1: RUF015 [*] Prefer `next(zip(x, y))` over single element slice
   |
56 | # RUF015 (zip)
57 | list(zip(x, y))[0]
58 | [*zip(x, y)][0]
   | ^^^^^^^^^^^^^^^ RUF015
59 | 
60 | # RUF015 (pop)
   |
   = help: Replace with `next(zip(x, y))`

ℹ Unsafe fix
55 55 | 
56 56 | # RUF015 (zip)
57 57 | list(zip(x, y))[0]
58    |-[*zip(x, y)][0]
   58 |+next(zip(x, y))
59 59 | 
60 60 | # RUF015 (pop)
61 61 | list(x).pop(0)

RUF015.py:61:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
   |
60 | # RUF015 (pop)
61 | list(x).pop(0)
   | ^^^^^^^^^^^^^^ RUF015
62 | [i for i in x].pop(0)
63 | list(i for i in x).pop(0)
   |
   = help: Replace with `next(iter(x))`

ℹ Unsafe fix
58 58 | [*zip(x, y)][0]
59 59 | 
60 60 | # RUF015 (pop)
61    |-list(x).pop(0)
   61 |+next(iter(x))
62 62 | [i for i in x].pop(0)
63 63 | list(i for i in x).pop(0)
64 64 | 

RUF015.py:62:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
   |
60 | # RUF015 (pop)
61 | list(x).pop(0)
62 | [i for i in x].pop(0)
   | ^^^^^^^^^^^^^^^^^^^^^ RUF015
63 | list(i for i in x).pop(0)
   |
   = help: Replace with `next(iter(x))`

ℹ Unsafe fix
59 59 | 
60 60 | # RUF015 (pop)
61 61 | list(x).pop(0)
62    |-[i for i in x].pop(0)
   62 |+next(iter(x))
63 63 | list(i for i in x).pop(0)
64 64 | 
65 65 | # OK

RUF015.py:63:1: RUF015 [*] Prefer `next(iter(x))` over single element slice
   |
61 | list(x).pop(0)
62 | [i for i in x].pop(0)
63 | list(i for i in x).pop(0)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ RUF015
64 | 
65 | # OK
   |
   = help: Replace with `next(iter(x))`

ℹ Unsafe fix
60 60 | # RUF015 (pop)
61 61 | list(x).pop(0)
62 62 | [i for i in x].pop(0)
63    |-list(i for i in x).pop(0)
   63 |+next(iter(x))
64 64 | 
65 65 | # OK
66 66 | list(x).pop(1)

RUF015.py:73:5: RUF015 [*] Prefer `next(iter(zip(x, y)))` over single element slice
   |
71 | def test():
72 |     zip = list  # Overwrite the builtin zip
73 |     list(zip(x, y))[0]
   |     ^^^^^^^^^^^^^^^^^^ RUF015
   |
   = help: Replace with `next(iter(zip(x, y)))`

ℹ Unsafe fix
70 70 | 
71 71 | def test():
72 72 |     zip = list  # Overwrite the builtin zip
73    |-    list(zip(x, y))[0]
   73 |+    next(iter(zip(x, y)))
