Understanding Lists as Ordered Collections
Indices start at 0 and can be positive (counting from the start) or negative (counting from the end).
Finding One Item
A list keeps values in an order, so you can ask for a particular element by referring to its index. The bracket operator connects the list with that index: my_list[index] retrieves a value, while my_list[index] = value changes the value stored at that position. The main challenge is remembering how Python numbers positions from the beginning and from the end.
Mapping Positions to Indices
Consider the generated list ["red", "green", "blue"]. From the beginning, the first element has index 0, the second has index 1, and the third has index 2. Python also lets you count backward: -1 refers to the last element, -2 to the element before it, and -3 to the first element. Positive and negative indices can therefore identify the same element from different directions.
The first element is index 0, not index 1. The last element can be reached with a positive index from the beginning or with index -1 from the end.
Reading an Element
Reading from Both Directions
Given the generated list ["Monday", "Tuesday", "Wednesday"], determine which value each expression retrieves: days[0], days[2], days[-1], and days[-3].
days[0]: Index 0 identifies the first element, so this expression retrieves Monday.
days[2]: Index 2 identifies the third element when counting from the beginning, so this expression retrieves Wednesday.
days[-1]: Index -1 identifies the last element when counting backward, so this expression also retrieves Wednesday.
days[-3]: Index -3 identifies the third element counted backward in this three-element list, so this expression retrieves Monday.
The positive and negative forms can point to the same element: days[0] and days[-3] identify Monday, while days[2] and days[-1] identify Wednesday.
first_day is "Monday" and last_day is "Wednesday".Changing One Position
Lists are mutable, which means individual elements can be changed without recreating the entire list. Put the target index on the left side of an assignment and the replacement value on the right side. Only the element at that index is reassigned; the other list elements remain in their existing positions.
Accessing and modifying use similar bracket notation, but their roles differ: my_list[index] reads a value, while my_list[index] = value replaces the value at that position.
Handling Invalid Indices
An index is valid only when it identifies an element that exists in the list. If an access expression refers to an index that does not exist, Python raises an IndexError instead of returning a value. Debugging begins by checking the list's actual elements and then checking whether the chosen index counts to an existing position from the beginning or from the end.
The first access retrieves "cat". The second access raises IndexError because index 2 does not identify an element in this list.Mistakes with List Positions
Treating the first element as index 1
Indices start at 0, so index 1 refers to the second element rather than the first.
Fix:
Use items[0] when you need the first element.Forgetting that negative indices count from the end
The expression does not refer to the first element; -1 refers to the last element.
Fix:
Use a non-negative index for counting from the beginning, or use -1 deliberately when you need the last element.Using an index that does not exist
Index 2 does not identify an element in this two-element generated list, so the access raises IndexError.
Fix:
Verify that the requested index identifies an existing position before accessing the list.Assigning to the list name instead of an indexed position
This assignment does not target one element at a specific index.
Fix:
Use items[index] = value when the goal is to modify one list element.
Practice the Trace
For the generated list numbers = ["one", "two", "three", "four"], identify the value retrieved by each expression: numbers[0], numbers[3], numbers[-1], and numbers[-4]. Then determine the list after this assignment: numbers[1] = "TWO". Finally, identify which expression would raise IndexError: numbers[4] or numbers[-4].
Hints
- Write the positive indices below the list from left to right, beginning with 0.
- Write the negative indices below the list from right to left, beginning with -1.
- For the assignment, change only the element at index 1.
- Check whether each requested index identifies an existing element.
Practice Check
Use numbers = ["one", "two", "three", "four"] to verify the four accesses, the reassignment, and the invalid-index check.
Accesses: numbers[0] is one, numbers[3] is four, numbers[-1] is four, and numbers[-4] is one.
Reassignment: Changing numbers[1] replaces two with TWO while the other elements stay in their positions.
Invalid access: numbers[4] raises IndexError because index 4 is outside the available positions. numbers[-4] is valid because it identifies the first element.
After the reassignment, the list is ["one", "TWO", "three", "four"].
Essential Takeaways
- List indices begin at 0, so index 0 identifies the first element.
- Positive indices count from the beginning, while negative indices count backward from the end.
- Use my_list[index] to retrieve an element and my_list[index] = value to modify one element.
- Lists are mutable, so an individual element can be changed without recreating the entire list.
- An invalid index raises IndexError; check that the requested position exists before accessing it.
Key Takeaways
- A list's first element is at index 0.
- Positive indices count from the beginning, and negative indices count from the end.
- Bracket notation retrieves an element and can also target an element for reassignment.
- Lists are mutable, so one indexed value can change while the rest of the list stays in place.
- IndexError means the requested index does not identify an existing element.