Slicing Lists to Extract Subsets
Indices start at 0 and can be positive (counting from the start) or negative (counting from the end).
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 List as Positions
A list stores values in an ordered sequence. To work with one particular value, identify its position with an index. Python uses the bracket operator to access a value: my_list[index]. The index tells Python which list position to retrieve.
Counting from Zero
Positive indices count forward from the beginning of a list, and the first position is index 0. The next positions are indexed 1, 2, and so on. Therefore, the index is a position label rather than the human-style count of first, second, or third. For a list with four values, the positive indices are 0, 1, 2, and 3.
Finding One Value
Suppose a list contains the values red, green, blue, and yellow. Which value is selected by index 2?
Locate the index: The positive indices begin at 0, so the positions are 0 for red, 1 for green, 2 for blue, and 3 for yellow.
Retrieve the value: The bracket expression my_list[2] retrieves the value at index 2.
The selected value is blue.
Counting Backward
Negative indices count from the end of a list. Index -1 identifies the last value, -2 identifies the value before it, and counting continues backward. This gives you another way to identify a value when its distance from the end is more useful than its distance from the beginning.
What do you think happens?
A four-value list contains red, green, blue, and yellow in that order. Which value does the index -1 identify?
Reveal answer
Answer: yellow
Negative indices count backward from the end, and -1 identifies the last value.
For the same four-value list, index -4 reaches the first value, -3 reaches the second, -2 reaches the third, and -1 reaches the fourth. Positive and negative indices can therefore refer to the same list element from opposite directions.
Selecting a Subset
A list slice extracts a subset of the values instead of selecting only one position. Think of the slice as a range of list positions: you identify where the selected portion begins and where it ends. The same positive and negative index system applies when locating those positions.
For example, if the target subset is the middle portion of the four-value list, the selected result contains green and blue while red and yellow remain outside that subset. The important reasoning step is to map the intended beginning and ending positions before extracting the values.
Replacing One Value
The bracket operator also lets you modify a list. The form my_list[index] = value assigns a new value at the selected position. Lists are mutable, so an individual element can be changed without recreating the entire list.
Changing a Specific Position
A list contains red, green, and blue. Replace the value at index 1 with orange.
Map the index: Because counting begins at 0, index 1 identifies green, the second value.
Assign the replacement: The assignment my_list[1] = orange changes the value stored at that position.
Check the remaining positions: The values at indices 0 and 2 are not the target of the assignment, so the replacement affects only index 1.
The list now contains red, orange, and blue.
Finding Invalid Indices
An IndexError occurs when code tries to access an index that does not exist. The reliable debugging question is: which indices are valid for this list? Check the list's positions from the beginning and, when useful, from the end before using an index.
Treating the first element as index 1
Python indices start at 0, so index 1 identifies the second position.
Fix:
Label the first position 0, then count upward.Using an index that is not present
The requested position does not exist, so Python raises IndexError.
Fix:
Verify the valid index range before accessing or modifying the list.Forgetting that negative indices count from the end
Index -1 identifies the last value.
Fix:
Read negative indices backward: -1 is last, -2 is the value before last.Changing the wrong position
The assignment changes the value at the exact index supplied.
Fix:
Map the intended value to its index before using the assignment form.
Practice Check
A list contains the values Monday, Tuesday, Wednesday, and Thursday in that order. Identify the value at positive index 2, identify the value at negative index -1, choose an index that would cause IndexError, and state which assignment would replace Tuesday.
Hints
- Write the positive indices beneath the values, beginning with 0.
- For -1, start at the last value and count backward.
- Check whether the requested positive index is one of the positions that exists.
- Tuesday is the second value, so its positive index is 1.
- Use positive indices to count from the beginning of a list, with the first position labeled 0. Use negative indices to count backward from the end, with -1 identifying the last value. Use bracket notation to retrieve a value, and assign through the same indexed position to modify an individual list element. A slice selects a subset by working with a range of list positions. If an index does not exist, Python raises IndexError, so verify the valid range before accessing or changing a value.
Key Takeaways
- List indices start at 0, so index 0 identifies the first element.
- Positive indices count from the beginning, while negative indices count backward from the end.
- Bracket notation retrieves a value, and indexed assignment modifies an individual list element.
- List slicing selects a subset by using list positions as boundaries.
- An IndexError means the requested index does not exist; check the valid range before access.