IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

slice

def slice(end: Int) -> Slice

Constructs a slice from the beginning up to end.

A Slice holds the bounds that bracket slice syntax like lst[1:5] expresses. Constructing one directly is useful when the bounds are computed at run time. The end bound is exclusive.

indices() resolves the bound against a container's length and counts a negative bound back from the end. Subscripting without a step is stricter: its bounds must be non-negative, and lst[:-1] aborts when bounds checking is enabled (the default on CPU) and causes undefined behavior otherwise. Compute the bound from the container's length instead, like lst[: len(lst) - 1] to stop one element short of the end.

For example:

var numbers: List[Int] = [10, 20, 30, 40, 50]
var from_start = slice(3)
print(numbers[from_start]) # [10, 20, 30]
print(from_start.indices(5)) # (0, 3, 1)

Args:

  • ​end (Int): The exclusive upper bound.

Returns:

Slice: A Slice covering the indices 0 up to end.

def slice(start: Int, end: Int) -> Slice

Constructs a slice from start up to end.

The bounds are half-open: the slice covers start up to, but not including, end. indices() resolves the bounds against a container's length and counts negative bounds back from the end. Subscripting without a step is stricter: its bounds must be non-negative, and lst[1:-1] aborts when bounds checking is enabled (the default on CPU) and causes undefined behavior otherwise. Compute bounds from the container's length instead, like lst[1 : len(lst) - 1] to stop one element short of the end.

For example:

var numbers: List[Int] = [10, 20, 30, 40, 50]
var s = slice(1, len(numbers) - 1)

# Slicing the list copies the covered elements into a new list.
var middle = numbers[s]
print(middle) # [20, 30, 40]

# The bounds also resolve on their own, without a container.
print(s.indices(len(numbers))) # (1, 4, 1)

Args:

  • ​start (Int): The inclusive lower bound.
  • ​end (Int): The exclusive upper bound.

Returns:

Slice: A Slice covering the indices start up to end.

def slice(start: Optional[Int], end: Optional[Int], step: Optional[Int]) -> Slice

Constructs a slice from start to end stepping by step.

The bounds are half-open. A None bound means the beginning or the end of the container, in the direction the step moves, and a None step means 1. indices() resolves the bounds against a container's length and counts negative bounds back from the end. Step-less slicing doesn't resolve negatives: its bounds must be non-negative, so compute them from the container's length instead.

For example:

var numbers: List[Int] = [10, 20, 30, 40, 50]
var backwards = slice(None, None, -1)
print(numbers[backwards]) # [50, 40, 30, 20, 10]
print(backwards.indices(5)) # (4, -1, -1)

Args:

  • ​start (Optional[Int]): The inclusive lower bound, or None for the beginning in the step's direction.
  • ​end (Optional[Int]): The exclusive upper bound, or None for the end in the step's direction.
  • ​step (Optional[Int]): The stride between the covered indices, where None means 1.

Returns:

Slice: A Slice covering the indices start to end by step.