Python list slice
last modified November 16, 2026 Python 3.15
In this article we show how to create and use list slices in Python.
A list is a mutable, ordered collection of values whose elements are accessed by zero-based indexes. A slice is a portion of a list. Slicing extracts a range of elements and returns them as a new list; slices can also be used on the left side of an assignment to replace or delete parts of a list.
Slice syntax
A slice is written in square brackets with up to three integers separated by colons.
vals[start:stop:step]
The start index is the first element included in the slice. The
stop index is the first element not included; the slice
ends just before it. The step is the distance between the
selected elements. All three parts are optional and all of them can be
negative.
Because the stop index is excluded, the slice vals[a:b] has
b - a elements (when both indexes are within the list).
Start and stop indexes
In the first example, we create slices with explicit start and stop indexes.
vals = [-2, -1, 0, 1, 2, 3, 4, 5, 6] s1 = vals[0:5] print(s1) s2 = vals[2:len(vals)] print(s2) s3 = vals[3:4] print(s3, vals[3])
The program creates three slices of a list of nine integers.
s1 = vals[0:5]
The slice contains the elements with indexes 0, 1, 2, 3, and 4. The element with index 5 is not included.
s2 = vals[2:len(vals)]
The length of the list is one past the last valid index, so using it as the stop index produces a slice that goes to the end of the list.
s3 = vals[3:4]
A slice is always a list, even when it has one element. The
vals[3] indexing expression returns the element itself.
$ python start_end.py [-2, -1, 0, 1, 2] [0, 1, 2, 3, 4, 5, 6] [1] 1
Omitting indexes
If we omit the start index, the slice begins at the first element. If we omit the stop index, the slice goes up to the last element. The omitted step defaults to 1.
vals = [-2, -1, 0, 1, 2, 3, 4, 5, 6] print(vals[:3]) print(vals[3:]) print(vals[:]) n = 4 print(vals[:n] + vals[n:] == vals)
The vals[:3] slice contains the first three elements and
vals[3:] contains the rest. With both indexes omitted,
vals[:] creates a copy of the whole list.
print(vals[:n] + vals[n:] == vals)
Thanks to the excluded stop index, vals[:n] and
vals[n:] split a list into two parts that do not overlap and
together contain all elements.
$ python omit_indexes.py [-2, -1, 0] [1, 2, 3, 4, 5, 6] [-2, -1, 0, 1, 2, 3, 4, 5, 6] True
Negative indexes
Negative indexes count from the end of the list. The last element has index -1, the last but one has index -2, and so on.
vals = [1, 2, 3, 4, 5, 6, 7, 8] print(vals[-4:-1]) print(vals[-3:]) print(vals[:-2]) print(vals[-1:-4])
The vals[-4:-1] slice contains the elements from the fourth from
the end up to, but not including, the last one. vals[-3:] gives
the last three elements and vals[:-2] everything except the last
two.
With a positive step, the start must come before the stop. The
vals[-1:-4] slice starts at the last element and has nothing to
the right of it, so it is empty; we must write vals[-4:-1]
instead.
$ python negative_indexes.py [5, 6, 7] [6, 7, 8] [1, 2, 3, 4, 5, 6] []
Indexes out of range
Unlike indexing, slicing never raises an IndexError. Slice
indexes beyond the list boundaries are clamped to the list length.
vals = [1, 2, 3, 4, 5]
print(vals[2:100])
print(vals[-100:2])
print(vals[10:20])
try:
print(vals[10])
except IndexError as e:
print(f'error: {e}')
The vals[2:100] slice simply ends at the last element and
vals[-100:2] starts at the first one. A slice that lies entirely
outside the list is empty. Accessing a single element with
vals[10] fails.
$ python out_of_range.py [3, 4, 5] [1, 2] [] error: list index out of range
Step
The third part of a slice is the step. It allows us to take every n-th element from a list.
vals = [-2, -1, 0, 1, 2, 3, 4, 5, 6] print(vals[1:9:2]) print(vals[::2]) print(vals[1::3])
The vals[1:9:2] slice takes every second element starting with
index 1 and ending before index 9: the elements with indexes 1, 3, 5, and 7.
The vals[::2] slice takes every second element of the whole list,
and vals[1::3] takes every third element starting with the
second one.
$ python step.py [-1, 1, 3, 5] [-2, 0, 2, 4, 6] [-1, 2, 5]
Negative step
With a negative step, the slice goes from right to left. The start index must then be greater than the stop index; omitted start and stop mean the end and the beginning of the list.
vals = [-2, -1, 0, 1, 2, 3, 4, 5, 6] print(vals[::-1]) print(vals[5:0:-2]) print(vals[::-2]) print(vals[2:5:-1])
The vals[::-1] slice is a common idiom for a reversed copy of a
list. The vals[5:0:-2] slice starts at index 5 and takes every
second element going left; index 0 is excluded. The vals[::-2]
slice takes every second element starting from the last one. The
vals[2:5:-1] slice is empty because index 2 lies to the left of
index 5.
$ python negative_step.py [6, 5, 4, 3, 2, 1, 0, -1, -2] [3, 1, -1] [6, 4, 2, 0, -2] []
Slices are shallow copies
A slice creates a new list, but the elements themselves are not copied. Both lists refer to the same objects. This is called a shallow copy.
vals = [1, 2, 3] dup = vals[:] dup.append(4) print(vals, dup) grid = [[1, 2], [3, 4]] grid_copy = grid[:] grid_copy[0].append(99) grid_copy[1] = ['new'] print(grid) print(grid_copy)
Appending to the copy does not change the original list. With a nested list,
however, the inner lists are shared: grid_copy[0].append(99)
modifies the inner list that both grid and
grid_copy refer to. Replacing grid_copy[1] with a
new list affects only the copy. For an independent copy of nested data, use
copy.deepcopy.
$ python shallow_copy.py [1, 2, 3] [1, 2, 3, 4] [[1, 2, 99], [3, 4]] [[1, 2, 99], ['new']]
Slice assignment
A slice on the left side of an assignment replaces the selected elements with the elements of an iterable. The number of new elements may differ from the size of the slice; the list grows or shrinks accordingly.
vals = [1, 2, 3, 4, 5, 6, 7, 8] vals[1:3] = [20, 30] print(vals) vals[1:3] = [0] print(vals) vals[1:1] = ['a', 'b', 'c'] print(vals) vals[-2:] = [] print(vals)
First we replace two elements with two new values. Then we replace two elements with a single one, and the list shrinks.
vals[1:1] = ['a', 'b', 'c']
An empty slice selects no elements; assigning to it inserts the new elements at that position.
vals[-2:] = []
Assigning an empty list deletes the selected elements. This is equivalent to
del vals[-2:].
$ python slice_assign.py [1, 20, 30, 4, 5, 6, 7, 8] [1, 0, 4, 5, 6, 7, 8] [1, 'a', 'b', 'c', 0, 4, 5, 6, 7, 8] [1, 'a', 'b', 'c', 0, 4, 5, 6]
Assigning to an extended slice
A slice with a step other than 1 is called an extended slice. When we assign to an extended slice, the iterable must have exactly as many elements as the slice.
vals = [1, 2, 3, 4, 5, 6, 7, 8]
vals[::2] = [0, 0, 0, 0]
print(vals)
try:
vals[::2] = [10, 20]
except ValueError as e:
print(f'error: {e}')
The vals[::2] slice selects four elements, which we replace with
four zeros. The second assignment provides only two values and raises a
ValueError.
$ python extended_assign.py [0, 2, 0, 4, 0, 6, 0, 8] error: attempt to assign sequence of size 2 to extended slice of size 4
Slice objects
The slicing syntax creates a slice object behind the scenes. We
can create such objects with the built-in slice function, give
them names, and reuse them.
records = [
'Paul Smith 34',
'Lucy Brown 28',
'Robert Novak 51',
]
FIRST = slice(0, 10)
LAST = slice(10, 20)
AGE = slice(20, None)
for rec in records:
print(rec[LAST].strip(), rec[FIRST].strip(), int(rec[AGE]))
print(LAST)
print(slice(None, None, -1).indices(5))
The records have fixed-width fields. Named slices make the code that extracts
the fields more readable than repeated numeric ranges. None stands
for an omitted index.
print(slice(None, None, -1).indices(5))
The indices method computes the actual start, stop, and step for
a sequence of the given length. For a reversing slice of five elements, it
starts at index 4 and runs past index 0.
$ python slice_objects.py Smith Paul 34 Brown Lucy 28 Novak Robert 51 slice(10, 20, None) (4, -1, -1)
Slicing other sequences
The slicing syntax is not limited to lists. Strings, tuples, ranges, and bytes support it too, and each returns an object of its own type.
word = 'falcon' print(word[:3], word[::-1]) point = (4, 8, 2, 7) print(point[1:3]) nums = range(0, 100, 5) print(nums[2:6], list(nums[2:6])) data = b'\x01\x02\x03\x04' print(data[1:])
A string slice is a string, a tuple slice a tuple, and a byte slice a bytes
object. Slicing a range produces another range
without creating any list.
$ python other_sequences.py fal noclaf (8, 2) range(10, 30, 5) [10, 15, 20, 25] b'\x02\x03\x04'
Source
Common sequence operations – Python documentation
slice – Python built-in functions
In this article we have worked with list slices in Python. See also Python slice built-in function, Python list, and Python list remove.
Author
List all Python tutorials.