List Indexing and Slicing
A list is an ordered, mutable collection of items created with square brackets and commas
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?
Start at zero: The first item, tea, is at index 0.
Move to the next position: The second item, coffee, is at index 1.
Identify 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?
Locate the start: Index 1 identifies blue, so blue is included.
Continue before the stop: Index 2 identifies green, so green is included.
Stop 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.
Initial state: The list contains red at index 0 and blue at index 1.
Append: Appending green adds it to the end, producing ["red", "blue", "green"].
Delete: 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.
Inspect the initial order: The items begin in the order pear, apple, banana.
Arrange alphabetically: Alphabetical order places apple first, banana second, and pear third.
Read 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 |
Mistakes to Avoid
Using 1 as the first index
Python list indexes start at 0.
Fix:
Label the first item with index 0, the second with index 1, and continue from there.Including the stop position in a slice
The stop position marks where the selected range ends.
Fix:
Include the start position and the positions before the stop position.Assuming an index remains attached to an item forever
Removing an item can shift later items to new positions.
Fix:
Recheck the current list and its indexes after append, del, or sort.Forgetting that mutation affects the original list
Lists are mutable, and these operations modify the original list directly.
Fix:
Treat 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.