Indexing Lists with the Index Operator
List slicing uses the syntax t[start:stop] to extract a portion of a list, where start is included and stop is excluded.
A Slice Is a Range of Positions
A list slice extracts a portion of a list by specifying a start position and a stop position. The syntax is t[start:stop]. The start index belongs to the selected portion, but the stop index does not. This start-included, stop-excluded rule lets you describe exactly where a selection begins and where it ends.
Tracing t[1:3]
Selecting the middle elements
Given t = ['a', 'b', 'c', 'd', 'e'], determine the result of t[1:3].
Read the start: The start index is 1, so the selection begins with the element at index 1, which is 'b'.
Read the stop: The stop index is 3, so the element at index 3, 'd', is not included.
Collect the selected positions: The selected indices are 1 and 2, containing 'b' and 'c'.
t[1:3] produces ['b', 'c'].
['b', 'c']Omitted Slice Boundaries
A slice does not require both boundary indices. If the start is omitted, the slice begins at the beginning of the list. If the stop is omitted, the slice continues from the given start index onward. If both are omitted, t[:] creates a copy of the entire list.
| Expression | Meaning |
|---|---|
| t[:n] | The first n elements |
| t[n:] | Elements from index n onward |
| t[:] | A copy of the entire list |
Shortcuts created by omitting slice indices
['a', 'b', 'c']
['d', 'e']
['a', 'b', 'c', 'd', 'e']Replacing a Slice In Place
Slice assignment uses the form t[start:stop] = new_list. Instead of extracting elements, this operation replaces the selected slice in the original list. For example, t[1:3] selects the elements at indices 1 and 2, so assigning ['x', 'y'] replaces those two elements while the other elements remain unchanged.
t = ['a', 'b', 'c', 'd'] t[1:3] = ['x', 'y'] print(t)
When the List Changes Size
The replacement list does not have to be the same length as the slice being replaced. A longer replacement makes the original list grow, while a shorter replacement makes it shrink. In t[1:3] = ['x', 'y', 'z'], two selected elements are replaced by three elements, so the list gains one element. Elements after the replaced slice shift to make room.
['a', 'x', 'y', 'z', 'd', 'e', 'f']Steps Beyond the Basic Slice
The full slice syntax is t[start:stop:step]. If 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']Common Slice Mistakes
Including the stop index in the result
The stop index is excluded. The slice includes indices 1 and 2 only.
Fix:
Read the expression as starting at index 1 and ending immediately before index 3.Treating t[:n] as including index n
t[:n] selects the first n elements, which are the positions before index n.
Fix:
For t[:3], count positions 0, 1, and 2.Assuming slice assignment must preserve list length
The replacement has three elements, while the selected slice has two.
Fix:
Compare the replacement length with the selected slice length. A longer replacement grows the list.Forgetting that slice assignment modifies the original list
Slice assignment replaces elements in the original list.
Fix:
Inspect the list after the assignment; elements outside the slice remain, while selected elements are replaced.
Predict Before Running
What do you think happens?
What does this code print?
Reveal answer
Answer: ['b', 'c']
The slice starts at index 1 and stops before index 3, so it selects indices 1 and 2.
For t = ['a', 'b', 'c', 'd', 'e', 'f'], predict the result of t[:2], t[4:], and t[::2]. Then run the expressions and compare the outputs with your predictions.
Hints
- For t[:2], use the first two elements.
- For t[4:], begin at index 4 and continue to the end.
- For t[::2], use a step of 2 and begin at the start because the start and stop are omitted.
Start with t = ['a', 'b', 'c', 'd', 'e']. Predict the final list after t[1:4] = ['x']. Then verify it by running the code. Explain why the list becomes shorter.
Hints
- The selected slice contains the elements at indices 1, 2, and 3.
- The replacement list contains one element.
- A shorter replacement causes the original list to shrink.
Slice Checklist
- Write the slice as start, stop, and optional step.
- Identify the element at the start index, if a start is present.
- Stop immediately before the stop index, if a stop is present.
- If a boundary is omitted, apply the corresponding beginning or end shortcut.
- For slice assignment, count the selected elements and compare that count with the replacement list.
- Predict the result before running the code, then execute the code to verify the prediction.
Key Takeaways
- List slicing uses t[start:stop], includes start, and excludes stop.
- Omitting start selects from the beginning, omitting stop selects through the end, and t[:] creates a copy of the entire list.
- Slice assignment replaces multiple elements in the original list.
- A replacement list can have a different length, causing the original list to grow or shrink.
- The optional step controls how many elements are skipped, and predicting slices before execution strengthens understanding.