Understanding List Indexing
Slicing extracts a contiguous subset of a list using the syntax list[start:stop:step], where start is inclusive, stop is exclusive, and step is optional (default 1).
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…
From One Position to Many
List indexing retrieves one element at a specific position. List slicing retrieves several elements by describing a range of positions. The general slice notation is list[start:stop:step]. The start position is included, the stop position is excluded, and the step controls how far the slice moves between selected positions.
The most important rule is that slicing includes start but excludes stop. A slice from index 1 to index 3 selects positions 1 and 2, not position 3.
Mapping Positions to Elements
To reason about a slice, first map each index to its element. Consider the six-element list t containing a, b, c, d, e, and f. Its positive indices begin at 0 and increase from left to right. The same elements also have negative indices counted from the right: -1 is the last element, -2 is the second-to-last, and so on.
Negative indices do not create a different sequence. They provide another way to name the same positions. For example, the final element can be reached with index 5 in this six-element list or with index -1. This makes negative indices useful when you want to work from the end without using the exact list length.
Tracing the Slice Boundary
A reliable way to predict a slice is to mark the starting position, move toward the stopping position, and leave the stop position out. For t[1:3], begin at index 1, include the element there, then include index 2. Stop before index 3. The result is b and c.
Reading t[1:3]
Determine which elements are selected from the six-element list t containing a, b, c, d, e, and f.
- 1Start at index 1
Index 1 contains b, and the start boundary is inclusive, so b is selected.
- 2Move to index 2
Index 2 contains c, so c is also selected.
- 3Reach index 3
Index 3 contains d, but the stop boundary is exclusive, so d is not selected.
The slice contains b and c.
Combining Start Stop and Step
The three positions in list[start:stop:step] have separate jobs. Start identifies the first position considered, stop identifies the boundary where selection ends, and step determines how many positions to move between selected elements. All three parameters are optional. When step is omitted, it defaults to 1, so each position in the selected range is visited.
With a step of 2, the slice selects every other element. For the list t, t[::2] begins at the default starting boundary, advances through indices 0, 2, and 4, and returns a, c, and e. A step of 3 would select every third element. A negative step can be used to reverse the order of selected elements.
What do you think happens?
Which elements are selected by t[1:5:2] when t contains a, b, c, d, e, and f?
Reveal answer
b and d
The slice starts at index 1, includes that position, advances by 2 to index 3, and stops before index 5. Indices 1 and 3 contain b and d.
Leaving Boundaries Empty
An omitted start tells Python to begin at index 0. An omitted stop tells Python to continue through the end of the list. Therefore, t[:4] selects the first four elements, while t[3:] selects every element from index 3 onward. Omitting both start and stop, as in t[:], spans the entire list.
Reading from the End
Negative slice boundaries use the same inclusive-start and exclusive-stop rules as positive boundaries. In t[1:-1], the slice begins at index 1 and stops before the final element. This selects every element except the first and last: b, c, d, and e.
Reading t[1:-1]
Determine the selected elements when t contains a, b, c, d, e, and f.
- 1Locate the start
Index 1 contains b, and the start is included.
- 2Interpret the stop
Index -1 refers to f, the final element. Because stop is exclusive, f is not included.
- 3Collect the range
The selected positions are the elements from b through e.
The slice contains b, c, d, and e.
When using a negative boundary, translate it into an element before deciding what the slice returns. Remember that -1 names the final element, but a stop of -1 excludes that element.
A Complete Prediction Routine
Use this routine for any slice: first identify the start, then identify the exclusive stop, and finally move by the step. If a boundary is missing, apply its default. If the step is missing, use 1. At each visited position, include the element unless that position is the stop boundary.
Tracing t[::2]
Predict the result for the six-element list t containing a, b, c, d, e, and f.
- 1Apply the omitted start
Because start is omitted, begin at index 0.
- 2Apply the omitted stop
Because stop is omitted, continue toward the end of the list.
- 3Apply the step
The step is 2, so visit indices 0, 2, and 4.
The slice returns a, c, and e.
Mistakes Beginners Make
Common mistake Including the stop position
What it looks likeTreating t[1:3] as if it included index 3
Why it failsThe stop boundary is exclusive.
Do this insteadSelect indices 1 and 2, so the result is b and c.
Common mistake Treating -1 as the first position
What it looks likeReading -1 as the beginning of the list
Why it failsNegative indices count from the end, and -1 refers to the last element.
Do this insteadTranslate negative indices from the right before tracing the slice.
Common mistake Forgetting that step skips positions
What it looks likeExpecting t[::2] to contain every element
Why it failsA step of 2 selects every other element.
Do this insteadTrace indices 0, 2, and 4 for t[::2].
Common mistake Assuming a slice changes the original list
What it looks likeExpecting a modified slice to modify the source list
Why it failsSlicing creates a new list containing the selected elements.
Do this insteadTreat the sliced list as a separate list object.
Practice the Trace
A list t contains a, b, c, d, e, and f. Predict the result of t[2:-1:2]. Explain which indices you visit, which boundary is excluded, and how the step changes the traversal.
Hints
- Translate index 2 and negative index -1 into elements or positions.
- Include the start position but stop before the position named by -1.
- Begin at index 2 and advance by 2.
Describe what happens when the start and stop are both omitted but the step is 3. State the default boundaries and identify the pattern of selected positions.
Hints
- An omitted start begins at index 0.
- An omitted stop extends to the end.
- A step of 3 selects every third position.
Slice Rules to Remember
- 1Slicing uses list[start:stop:step] to create a new list from selected elements.
- 2The start boundary is inclusive, while the stop boundary is exclusive.
- 3An omitted start means index 0, and an omitted stop means the end of the list.
- 4Negative indices count from the end, with -1 naming the final element.
- 5The step controls the positions visited; the default step is 1.
Key Takeaways
- A slice selects multiple list elements using start, stop, and optional step parameters.
- Always include the start position and exclude the stop position when tracing a slice.
- Missing boundaries use the beginning or end of the list, while a missing step defaults to 1.
- Negative indices name positions from the end of the list.
- Slicing creates a new list and does not modify the original list.