Python foundations

Python Slicing: Start, Stop, Step, and Negative Indexes

Slice Python lists or strings with start, stop, and step. Read negative-step examples and see what a list slice copies.

Use sequence[start:stop:step] to take part of a Python list or string. The slice includes the start and excludes the stop. A step of one is the default, so "garden"[1:4] selects indexes 1, 2, and 3 and returns "ard".

labels = ["north", "south", "east", "west"]
assert labels[1:3] == ["south", "east"]
assert labels[:2] == ["north", "south"]
assert labels[-2:] == ["east", "west"]
assert labels[::2] == ["north", "east"]

Built-in lists and strings clip slice bounds to the available items. If labels has four entries, labels[:99] returns all four. Indexing a single missing position, as in labels[99], raises IndexError.

Use the boundary that matches the question

For “up to three items,” items[:3] naturally gives an empty list, a short list, or its first three entries. If your function promises exactly three values, check the length explicitly. A slice returning a shorter list is a perfectly valid Python result.

Use adjacent slices to split a sequence without skipping or copying a boundary item twice. items[:split] contains everything before the boundary. items[split:] contains everything from the boundary onward. For an ordinary list, concatenating those slices in order gives the same values as the original. Validate the requested split if negative or oversized positions should be rejected by your application.

Reverse a sequence with a negative step

A negative step moves toward smaller indexes. With omitted bounds, items[::-1] produces a reversed list. With explicit indexes, items[3:0:-1] takes positions three, two, and one, omitting zero. To include position zero, leave the stop empty in that example.

An explicit stop of -1 points to the last item. For any nonempty list, items[:-1:-1] returns an empty list because it starts at that item and stops before it. Leave the stop out in items[::-1] to reverse the whole list. A step of zero raises ValueError.

Know how much a list slice copies

copy = values[:] creates a new list with references to the original list's items. Adding or removing slots in one list does not add or remove slots in the other. If an item is mutable, changes to that same item can still appear through both lists.

groups = [["first"], ["second"]]
subset = groups[:1]
subset[0].append("shared edit")
assert groups[0] == ["first", "shared edit"]
assert len(groups) == 2

For a large list, taking a slice copies references for the selected range. If you only need to iterate over a portion, consider whether a generator or itertools.islice fits your input. Its bounds are not a drop-in replacement for every slice, especially if you need negative indexes or reversal.

Separate reading a slice from assigning one

items[1:3] produces a result. items[1:3] = replacements changes the original list and may change its length for a simple step-one slice. A stepped slice assignment requires the replacement count to match the number of selected items. Strings are immutable, so slice assignment does not work on strings.

Test the smallest sequence your caller sees, including an empty input. A short sample should show whether you meant to include the stop, handle an oversized request, or move backward through the list. For a reusable public function, document whether an out-of-bounds slice is accepted or signals an invalid request.

Sources and further reading

Related reading

Browse Python foundations · Explore project tutorials

Search PythonProjects.org