Language: EN

python-slices

How to use Slices in Python

Slices in Python are a very useful tool, which allows us to extract portions of sequences such as lists, tuples, or strings with very little code.

Slices allow us to work with specific fragments of a sequence without having to modify the original.

The basic syntax for creating a slice in Python is as follows:

sequence[start:end:step]
  • start: Index where the slice starts (included).
  • end: Index where the slice ends (excluded).
  • step: Size of the step or increment between elements of the slice (optional).

Special Considerations

Omission of values

We can omit one, several, or even all the parameters of the Slice. This will behave differently for each omission. If we do not specify:

  • The start, the slice will start from the first element
  • The end, the slice will end in the last element
  • The step, the slice will use a step of 1
first_except_three = numbers[3:]  # Extracts the elements from 3 to the end
first_three = numbers[:3]  # Extracts the first three elements

See below the examples to understand the concepts better 👇

Use of negative indexes

Python allows the use of negative indexes to refer to elements relative to the end of the sequence, rather than from the beginning.

It seems a bit complicated, and at first it gets confusing. But basically,

last_three = numbers[-3:]  # Extracts the last three elements
first_except_three = numbers[:-3]  # Extracts the elements, except the last three

On the other hand, if the step is negative, the Slice traverses the sequence in reverse order.

Again, see below the examples to understand the concepts better 👇

Modifying values with Slices

Slices can also be used to modify values in a sequence.

fruits = ["apple", "banana", "cherry", "date", "grape"]

fruits[1:3] = ["pear", "orange"]  # Replaces "banana" and "cherry" with "pear" and "orange"

In this case,

  • fruits[1:3] selects the elements "banana" and "cherry"
  • It replaces them with "pear" and "orange".

Examples of Slices

Suppose we have a list of numbers:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Now, let’s see how Slicing behaves with different combinations of start, end, and step:

Without specifying start, end, or step

result = numbers[:]
print(result)  # Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In this case, the slice copies the entire original list, as no value is specified for start, end, or step.

Specifying start and end

result = numbers[2:7]
print(result)  # Result: [2, 3, 4, 5, 6]

Here, the slice starts at index 2 and ends at index 6 (non-inclusive), using the default step of 1.

Specifying only end

result = numbers[:5]
print(result)  # Result: [0, 1, 2, 3, 4]

In this case, the Slice starts from the first element and ends at index 5 (non-inclusive), using the default step of 1.

Specifying only start

result = numbers[3:]
print(result)  # Result: [3, 4, 5, 6, 7, 8, 9]

Here, the Slice starts at index 3 and continues to the last element of the list, using the default step of 1.

Specifying only step

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

even_numbers = numbers[::2]  # Result: [0, 2, 4, 6, 8]

Here, numbers[::2] creates a new slice of the numbers list with a step of 2, meaning it extracts every second element, resulting in [0, 2, 4, 6, 8].

Specifying all

result = numbers[1:8:2]
print(result)  # Result: [1, 3, 5, 7]

In this example, the slice starts at index 1, ends at index 8 (non-inclusive), and uses a step of 2, taking every second element from the start to the specified end.

Negative indexes at the beginning

By using negative indexes at the beginning of the slice, elements are extracted from the end of the list.

last_three = numbers[-3:]  # Extracts the last three elements: [7, 8, 9]

In this case, numbers[-3:] creates a slice that includes the last three elements of the numbers list.

Negative indexes at the end

It is also possible to use negative indexes at the end of the slice to define the start from the end of the list.

without_last_three = numbers[:-3]  # Extracts all elements except the last three: [0, 1, 2, 3, 4, 5, 6]

Here, numbers[:-3] creates a slice that includes all elements of the numbers list except the last three.

Negative step

Using a negative step in the slice reverses the order of the extracted elements.

reversed = numbers[::-1]  # Reverses the order of the list: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

By specifying numbers[::-1], a slice is created that starts from the last element to the first, thus reversing the order of the original list.