Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Lists, Tuples and Ranges

Python slicing

Reference for Python slice syntax: start, stop and step, negative indexes, slice assignment and deletion, copies versus views, and slice objects.

A slice picks out a part of a sequence with the syntax sequence[start:stop:step]. It works on lists, tuples, strings, ranges, bytes and any class that supports it. The result is a new object of the same kind, containing the selected items.

Slicing is forgiving: indexes that fall outside the sequence are clamped instead of raising an error, so a slice never raises IndexError. This page explains how the three parts work, how negative values behave, and what you can do beyond reading.

Python
letters = ["a", "b", "c", "d", "e"]
print(letters[1:4])   # ['b', 'c', 'd']

Try it

Python Start, stop and step

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Copies and slice assignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How slices work

The three parts

start is the first index included, stop is the first index excluded, and step is the distance between picked items. All three are optional. The defaults depend on the sign of the step: with a positive step, start defaults to the beginning and stop to the end; with a negative step, they default to the end and the beginning, so the sequence is walked backward.

Because stop is excluded, items[a:b] has b - a items, and items[:n] + items[n:] rebuilds the original.

Negative indexes

A negative start or stop counts from the end: -1 is the last item position, -2 the one before. items[-3:] is the last three items. A step of -1 reverses the sequence, and 0 as a step raises ValueError.

Out-of-range values

Indexes larger than the length are clamped, and an empty selection gives an empty sequence. That makes items[:10] safe on a list with only three items. A single index such as items[10] is different: it raises IndexError.

Slices are copies

For lists, tuples, strings and bytes, a slice creates a new object. It is a shallow copy: the items themselves are shared, so nested lists are the same objects in both. items[:] is a quick way to copy a list. A range slice produces another range, and memoryview slices are views onto the same buffer rather than copies.

Slice assignment and deletion

Mutable sequences let you assign to a slice. The right side must be iterable and can have a different length, so the list grows or shrinks: a[1:3] = [...] replaces two items with any number. a[1:1] = [...] inserts without removing anything. With an extended slice (a step other than 1), the right side must have exactly the same number of items, or ValueError is raised. del a[1:3] removes a section.

Slice objects

slice(start, stop, step) builds the same selector as a value you can store and reuse. It has .start, .stop and .step attributes and a .indices(length) method that returns the concrete numbers for a given length. Classes support slicing by accepting a slice in __getitem__.

Usage notes

  • Strings are immutable, so you can slice them but not assign to a slice.
  • Slicing with a step skips items. items[::2] takes every second item starting at the first.
  • Common mistake: items[1:3] does not include index 3. Count positions between items to see where the cuts fall.
  • Copying nested data: a slice copy is shallow. Use copy.deepcopy() when inner lists must be independent.
  • Performance: a slice copies its items, which costs time proportional to its length. In hot loops over big lists, prefer indexes or itertools.islice().
  • NumPy arrays return views for basic slices, unlike lists. Changing the slice changes the array.