slice

In Python, a slice is an object that allows you to extract a portion of a sequence, such as a list, tuple, or string.

Slices are a powerful way to extract and manipulate parts of sequences. You create a slice by specifying a start, stop, and an optional step index. This enables you to extract a contiguous range of elements from a sequence without the need for looping or extensive indexing.

Syntax

The syntax that lets you get a slice from a sequence is the following:

Python Syntax
sequence[start:stop:step]

Example

Here’s an example of how you can use slicing to work with lists:

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

>>> # Slice from index 2 to 5
>>> subset = numbers[2:6]
>>> subset
[2, 3, 4, 5]
>>> # Slice with a step of 2
>>> step_slice = numbers[0:9:2]
>>> step_slice
[0, 2, 4, 6, 8]
>>> # Slice from index 4 to the end
>>> end_slice = numbers[4:]
>>> end_slice
[4, 5, 6, 7, 8, 9]
>>> # Reverse the list using slicing
>>> reversed_list = numbers[::-1]
>>> reversed_list
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Tutorial

Reverse Python Lists: Beyond .reverse() and reversed()

In this step-by-step tutorial, you'll learn about Python's tools and techniques to work with lists in reverse order. You'll also learn how to reverse your list by hand.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 25, 2025