Working with Sequences in Python
For t[1:3], Python starts at index 1, takes the element there, then continues through index 2.
A Boundary That Matters
A list slice looks small, but its boundaries control exactly which elements are selected. In the expression t[start:stop], the start index is included and the stop index is excluded. That single rule lets you select a precise portion of a list and is also the basis for changing several list elements at once.
What do you think happens?
Given the list ['a', 'b', 'c', 'd', 'e', 'f'], which elements does t[1:3] select?
Reveal answer
['b', 'c']
The slice begins at index 1 and includes that index. It stops before index 3, so it includes indices 1 and 2 only.
Mapping Boundaries to Elements
For t[1:3], Python starts at index 1, takes the element there, then continues through index 2. It stops before index 3. With t equal to ['a', 'b', 'c', 'd', 'e', 'f'], the result is ['b', 'c']. The stop value is a boundary rather than an included position.
Reading a Slice Step by Step
Determine the result of t[2:5] when t is ['a', 'b', 'c', 'd', 'e', 'f'].
- 1Read the start
The start index is 2, so the slice begins with 'c'.
- 2Read the stop
The stop index is 5, so index 5 and its value 'f' are not included.
- 3Collect the selected positions
The included indices are 2, 3, and 4, which contain 'c', 'd', and 'e'.
['c', 'd', 'e']
Shortcuts for List Portions
The start and stop indices are optional. Writing t[:n] selects the first n elements, because the slice begins at the start of the list and stops before index n. Writing t[n:] selects the elements from index n onward. Writing t[:] selects the entire list and creates a copy of it.
t = ['a', 'b', 'c', 'd', 'e', 'f']
first_three = t[:3]
from_index_three = t[3:]
whole_copy = t[:]- 1The omitted start in t[:3] means selection begins at the start of the list.
- 2The omitted stop in t[3:] means selection continues from index 3 onward.
- 3Both omitted indices in t[:] select the entire list as a copy.
- t[:3] selects the first three elements.
- t[3:] selects index 3 and every later element.
- t[:] selects the entire list and creates a copy.
first_three = ['a', 'b', 'c']
from_index_three = ['d', 'e', 'f']
whole_copy = ['a', 'b', 'c', 'd', 'e', 'f']Replacing a Slice
A slice can appear on the left side of an assignment. The form t[start:stop] = new_list replaces every element in the selected slice with the elements of new_list. This modifies the original list in place. Elements outside the selected slice remain unchanged, while the list can grow or shrink if the replacement has a different length.
Replacing Two Elements with Three
Apply t[1:3] = ['x', 'y', 'z'] to t = ['a', 'b', 'c', 'd', 'e', 'f'].
- 1Identify the slice
Indices 1 and 2 are selected. Their values are 'b' and 'c'; index 3 is excluded.
- 2Remove the selected values
The two selected elements are replaced, while 'a', 'd', 'e', and 'f' stay in the list.
- 3Insert the replacement sequence
The three replacement values 'x', 'y', and 'z' occupy the slice position.
['a', 'x', 'y', 'z', 'd', 'e', 'f']
['a', 'x', 'y', 'z', 'd', 'e', 'f']The original slice contains two elements, but the replacement contains three. Therefore, the list grows from six elements to seven. If the replacement list were shorter than the selected slice, the list would shrink instead.
Extending the Mental Model
The full slice syntax is t[start:stop:step]. The step controls how many elements to skip between selections. If the step is omitted, it defaults to 1, so every element in the selected range is included. A step of 2 selects every other element, and a step of 3 selects every third element. A negative step reverses the selection.
['a', 'c', 'e']
['a', 'd']Mistakes Beginners Make
Common mistake Including the stop index in the result
What it looks likeExpecting t[1:3] to return ['b', 'c', 'd']Why it failsThe stop index is excluded, so index 3 and the value at that position are not selected.
Do this insteadTreat t[1:3] as the range containing indices 1 and 2.
Common mistake Confusing t[:n] with t[n:]
What it looks likeTreating t[:3] and t[3:] as equivalentWhy it failsThe first form selects the first n elements, while the second starts at index n and continues onward.
Do this insteadUse t[:n] for the beginning portion and t[n:] for the portion beginning at index n.
Common mistake Assuming slice assignment must preserve list length
What it looks likeExpecting t[1:3] = ['x', 'y', 'z'] to leave the list at six elements
Why it failsThe replacement contains three elements while the selected slice contains two.
Do this insteadCompare the replacement length with the selected slice length. A longer replacement grows the list, and a shorter replacement shrinks it.
Common mistake Forgetting that slice assignment modifies the original list
What it looks likeUsing t[1:3] = ['x', 'y'] and expecting a separate result without changing t
Why it failsSlice assignment replaces elements in the original list in place.
Do this insteadExpect t itself to contain the replacement values after the assignment.
Predict Before Running
For t = ['a', 'b', 'c', 'd', 'e', 'f'], predict the result of each expression before checking it in Python: t[1:4], t[:2], t[4:], and t[::2]. Then predict the final list after t[2:4] = ['x'].
Hints
- For each two-index slice, include the start index and stop before the stop index.
- An omitted start begins at the beginning of the list.
- An omitted stop continues to the end of the list.
- A step of 2 selects every other element.
- For slice assignment, compare the number of selected elements with the number of replacement elements.
t = ['a', 'b', 'c', 'd', 'e', 'f']
print(t[1:4])
print(t[:2])
print(t[4:])
print(t[::2])
t[2:4] = ['x']
print(t)- 1t[1:4] returns ['b', 'c', 'd'] because index 4 is excluded.
- 2t[:2] returns ['a', 'b'] because it selects the first two elements.
- 3t[4:] returns ['e', 'f'] because selection begins at index 4 and continues to the end.
- 4t[::2] returns ['a', 'c', 'e'] because the step selects every other element.
- 5t[2:4] selects 'c' and 'd'. Replacing those two elements with ['x'] makes the list shorter.
- The first expression selects indices 1 through 3.
- The second expression selects the first two elements.
- The third expression selects index 4 onward.
- The fourth expression uses a step of 2.
- The assignment replaces indices 2 and 3 with one element.
['b', 'c', 'd']
['a', 'b']
['e', 'f']
['a', 'c', 'e']
['a', 'b', 'x', 'e', 'f']The final check confirms both parts of the mental model: t[2:4] selects indices 2 and 3, and replacing two elements with one causes the list to shrink. Prediction is useful because it exposes an incorrect boundary assumption before that assumption causes a harder debugging problem.
Key Takeaways
- 1In t[start:stop], start is included and stop is excluded.
- 2t[:n] selects the first n elements, t[n:] selects from index n onward, and t[:] creates a copy of the entire list.
- 3Slice assignment replaces multiple elements in the original list.
- 4A replacement sequence can make the list grow or shrink when its length differs from the selected slice.
- 5The full form t[start:stop:step] supports skipped elements, including reverse selection with a negative step.
At a glance
- A slice includes its start index but excludes its stop index.
- Omitted boundaries provide shortcuts for selecting the beginning, end, or entire list.
- Slice assignment changes the original list and can change its length.
- Predicting slice results before execution strengthens understanding of index boundaries.
- The optional step selects elements at intervals and can reverse selection when negative.