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.
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
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].
- 1Read the start
The start index is 1, so the selection begins with the element at index 1, which is 'b'.
- 2Read the stop
The stop index is 3, so the element at index 3, 'd', is not included.
- 3Collect 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)- 1The original list contains 'b' and 'c' at the selected positions.
- 2The replacement list contains 'x' and 'y'.
- 3The final list keeps 'a' and 'd' and places the replacement elements between them.
- The slice t[1:3] identifies the elements at indices 1 and 2.
- The assignment replaces those selected elements with 'x' and 'y'.
- The elements outside the slice remain in the list.
['a', 'x', 'y', 'd']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
Common mistake Including the stop index in the result
What it looks likeExpecting t[1:3] to include the element at index 3
Why it failsThe stop index is excluded. The slice includes indices 1 and 2 only.
Do this insteadRead the expression as starting at index 1 and ending immediately before index 3.
Common mistake Treating t[:n] as including index n
What it looks likeExpecting t[:3] to contain four elements
Why it failst[:n] selects the first n elements, which are the positions before index n.
Do this insteadFor t[:3], count positions 0, 1, and 2.
Common mistake Assuming slice assignment must preserve list length
What it looks likeExpecting t[1:3] = ['x', 'y', 'z'] to leave the list at its original length
Why it failsThe replacement has three elements, while the selected slice has two.
Do this insteadCompare the replacement length with the selected slice length. A longer replacement grows the list.
Common mistake Forgetting that slice assignment modifies the original list
What it looks likeUsing t[1:3] = ['x', 'y'] and then expecting t to remain unchanged
Why it failsSlice assignment replaces elements in the original list.
Do this insteadInspect 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
['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
- 1Write the slice as start, stop, and optional step.
- 2Identify the element at the start index, if a start is present.
- 3Stop immediately before the stop index, if a stop is present.
- 4If a boundary is omitted, apply the corresponding beginning or end shortcut.
- 5For slice assignment, count the selected elements and compare that count with the replacement list.
- 6Predict 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.