Programming Search⌘K

Featurecore

Slicing

Also called slice, slice notation, start stop step, negative index, reverse a list, slice assignment, slice object, reversed slice.

s[i:j:k] takes the items from index i up to but not including j, with step k. Any of the three can be left out, and negative values count from the end. A slice of a list is a new list.

Language
Python

Synopsis

names = ["zero", "one", "two", "three", "four", "five"]
print(names[1:3])
print(names[:3], names[3:])
print(names[::2])
print(names[-2:], names[::-1][:2])
Output
['one', 'two']
['zero', 'one', 'two'] ['three', 'four', 'five']
['zero', 'two', 'four']
['four', 'five'] ['five', 'four']

The first number is the start, the second is the stop, and the third is the step. The stop item is not in the result.

Semantics

r1. "In this form, the subscript is a slice: up to three expressions separated by colons. Any of the expressions may be omitted, but a slice must contain at least one colon".1

r2. "The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j."2 "If i is omitted or None, use 0. If j is omitted or None, use len(s)."2

r3. "If i or j is less than -len(s), use 0. If i or j is greater than len(s), use len(s)."2 So a slice never raises IndexError. Example: clamp.

r4. "If i or j are omitted or None, they become “end” values (which end depends on the sign of k). Note, k cannot be zero."2 Example: zero-step.

r5. "When a slice is evaluated, the interpreter constructs a slice object whose start, stop and step attributes, respectively, are the results of the expressions between the colons. Any missing expression evaluates to None."1 Example: slice-object.

r6. "All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list".3 Example: copy.

r7. "Assignment to slices is also possible, and this can even change the size of the list or clear it entirely".3 For an extended slice, "If k is not equal to 1, t must have the same length as the slice it is replacing."2 Examples: assign, extended-length.

Examples

s = [1, 2, 3]
print(s[1:100], s[-100:2], s[5:])
Output
[2, 3] [1, 2] []

Bounds outside the list are moved to the ends (rule r3). The index s[5] would raise IndexError.

letters = ["a", "b", "c", "d", "e", "f", "g"]
letters[2:5] = ["C", "D", "E"]
print(letters)
letters[2:5] = []
print(letters)
letters[1:1] = ["x", "y"]
print(letters)
letters[::2] = [0, 0, 0]
print(letters)
Output
['a', 'b', 'C', 'D', 'E', 'f', 'g']
['a', 'b', 'f', 'g']
['a', 'x', 'y', 'b', 'f', 'g']
[0, 'x', 0, 'b', 0, 'g']

The empty slice [1:1] inserts items. The extended slice [::2] replaces every second item (rule r7).

middle = slice(1, -1)
word = "python"
nums = [1, 2, 3, 4]
print(word[middle], nums[middle])
print(middle.start, middle.stop, middle.step)
print(slice(None, None, -1).indices(4))
Output
ytho [2, 3]
1 -1 None
(3, -1, -1)

A program can store a slice object and use it on many sequences (rule r5).

original = [[1], [2]]
copy = original[:]
copy.append([3])
copy[0].append(99)
print(original)
print(copy)
Output
[[1, 99], [2]]
[[1, 99], [2], [3]]

The slice is a new list, but it holds the same inner lists (rule r6).

Pitfalls

A step of zero is an error:

This code raises ValueError, on purpose.

"abc"[::0]

An extended slice assignment needs the same number of items:

This code raises ValueError, on purpose.

nums = [1, 2, 3, 4]
nums[::2] = [9]

Comparison

A Go slice expression a[low:high] has no step and no negative indices, and the result shares memory with the operand: "if the result is a slice, it shares its underlying array with the operand."5 A Python slice of a list is a new list (rule r6), so a change to the slice does not change the original list.

References

  1. The Python Language Reference, Python 3.15, 6.3.2.1. Slicings
  2. Built-in Types, Python 3.15 documentation, Common Sequence Operations, notes 3 to 5
  3. The Python Tutorial, 3. An Informal Introduction to Python, 3.1.3. Lists
  4. Built-in Functions, Python 3.15 documentation, slice()
  5. The Go Programming Language Specification, Slice expressions

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source