List Indexing and Slicing
For the generated list shown in the diagram, index 0 identifies red, index 1 identifies blue, index 2 identifies green, and index 3 identifies yellow. Indexing refers to positions, so changing the list can change which item belongs to a later index.
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…
One List, Many Positions
A Python list keeps items in an ordered sequence. You create the sequence with square brackets and commas, and each item occupies a position called an index. The first index is 0, not 1. Once you understand that position system, you can select one item, select a range of items, add new items, remove items, and rearrange the list.
For the generated list shown in the diagram, index 0 identifies red, index 1 identifies blue, index 2 identifies green, and index 3 identifies yellow. Indexing refers to positions, so changing the list can change which item belongs to a later index.
Selecting Items by Index
To access an individual item, identify the list and then identify its index position. Because indexing starts at 0, the item at index 0 is the first item, the item at index 1 is the second item, and so on. Indexing does not describe the item's value; it describes where the item appears in the ordered list.
Finding one item
Consider the ordered list ["tea", "coffee", "water"]. Which item is at index 1?
- 1Start at zero
The first item, tea, is at index 0.
- 2Move to the next position
The second item, coffee, is at index 1.
- 3Identify the result
The item at index 1 is coffee.
coffee
Selecting a Range
Slicing selects a range of items from an ordered list. A slice is described by a start index and a stop index. The selected range begins at the start position and continues through the positions before the stop position. This lets you work with several neighboring items without selecting each item separately.
Reading a slice
Consider the ordered list ["red", "blue", "green", "yellow"]. What items are selected by a slice beginning at index 1 and stopping at index 3?
- 1Locate the start
Index 1 identifies blue, so blue is included.
- 2Continue before the stop
Index 2 identifies green, so green is included.
- 3Stop at the boundary
Index 3 identifies yellow, but the stop position is not included.
["blue", "green"]
Changing List Contents
Lists are mutable, which means list operations modify the original list directly. The append method adds an item to the list. The del statement removes an item by its index. After an item is added or removed, the ordered contents and the index positions can change.
Tracking a mutation
Start with ["red", "blue"]. Add green, then remove the item at index 1.
- 1Initial state
The list contains red at index 0 and blue at index 1.
- 2Append
Appending green adds it to the end, producing ["red", "blue", "green"].
- 3Delete
Deleting index 1 removes blue. The remaining list is ["red", "green"].
The original list now contains red at index 0 and green at index 1.
Reordering with Sort
The sort method arranges the items in a list alphabetically. Sorting changes the original list directly because a list is mutable. As the order changes, the item found at each index can change too.
Following a sort
Start with ["pear", "apple", "banana"] and sort the list alphabetically.
- 1Inspect the initial order
The items begin in the order pear, apple, banana.
- 2Arrange alphabetically
Alphabetical order places apple first, banana second, and pear third.
- 3Read the new positions
After sorting, apple is at index 0, banana is at index 1, and pear is at index 2.
["apple", "banana", "pear"]
| Operation | Purpose | Effect on the original list |
|---|---|---|
| Indexing | Access one item by position | Does not describe a change |
| Slicing | Select a range of items | Selects a range from the ordered list |
| append | Add an item | Changes the list directly |
| del | Remove an item by index | Changes the list directly |
| sort | Arrange items alphabetically | Changes the list directly |
List operations have different purposes, but append, del, and sort modify the original mutable list.
Mistakes to Avoid
Common mistake Using 1 as the first index
What it looks likeTreating the first item as if it were at index 1
Why it failsPython list indexes start at 0.
Do this insteadLabel the first item with index 0, the second with index 1, and continue from there.
Common mistake Including the stop position in a slice
What it looks likeExpecting a slice from start index 1 to stop index 3 to include the item at index 3
Why it failsThe stop position marks where the selected range ends.
Do this insteadInclude the start position and the positions before the stop position.
Common mistake Assuming an index remains attached to an item forever
What it looks likeUsing an old index after deleting an earlier item
Why it failsRemoving an item can shift later items to new positions.
Do this insteadRecheck the current list and its indexes after append, del, or sort.
Common mistake Forgetting that mutation affects the original list
What it looks likeExpecting append, del, or sort to leave the original list unchanged
Why it failsLists are mutable, and these operations modify the original list directly.
Do this insteadTreat the list as changed immediately after one of these operations.
Practice the Position System
Consider the list ["oak", "pine", "maple", "birch"]. Identify the item at index 2, identify the items in the range beginning at index 1 and stopping at index 3, append "cedar", delete the item at index 0, and describe the new index of maple.
Hints
- Count the first item as index 0.
- A slice includes its start position and stops before its stop position.
- After deleting index 0, the remaining items move toward the beginning of the list.
When working with a list, first write down the current order and its zero-based indexes. After append, del, or sort, update that position map before accessing an item by index. This makes changes to the original mutable list easier to follow.
Key Takeaways
- A list is an ordered, mutable collection created with square brackets and commas.
- List indexes start at 0, so the first item is at index 0.
- Slicing selects a range beginning at a start index and ending before a stop index.
- append adds an item, del removes an item by index, and sort arranges items alphabetically.
- Because lists are mutable, append, del, and sort modify the original list directly and can change index positions.