Understanding Lists as Ordered Collections
Lists are mutable, which means individual elements can be changed without recreating the entire list.
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].
- 1days[0]
Index 0 identifies the first element, so this expression retrieves Monday.
- 2days[2]
Index 2 identifies the third element when counting from the beginning, so this expression retrieves Wednesday.
- 3days[-1]
Index -1 identifies the last element when counting backward, so this expression also retrieves Wednesday.
- 4days[-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.
colors = ["red", "green", "blue"]
colors[1] = "yellow"The value at index 1 is "green".
colors = ["red", "yellow", "blue"]The value at index 1 is "yellow".
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
Common mistake Treating the first element as index 1
What it looks likeitems = ["a", "b"]; items[1]Why it failsIndices start at 0, so index 1 refers to the second element rather than the first.
Do this insteadUse items[0] when you need the first element.
Common mistake Forgetting that negative indices count from the end
What it looks likeitems[-1]Why it failsThe expression does not refer to the first element; -1 refers to the last element.
Do this insteadUse a non-negative index for counting from the beginning, or use -1 deliberately when you need the last element.
Common mistake Using an index that does not exist
What it looks likeitems = ["a", "b"]; items[2]Why it failsIndex 2 does not identify an element in this two-element generated list, so the access raises
IndexError.Do this insteadVerify that the requested index identifies an existing position before accessing the list.
Common mistake Assigning to the list name instead of an indexed position
What it looks likeitems = "new value"Why it failsThis assignment does not target one element at a specific index.
Do this insteadUse 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.
- 1Accesses
numbers[0] is one, numbers[3] is four, numbers[-1] is four, and numbers[-4] is one.
- 2Reassignment
Changing numbers[1] replaces two with TWO while the other elements stay in their positions.
- 3Invalid access
numbers[4] raises
IndexErrorbecause 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
- 1List indices begin at 0, so index 0 identifies the first element.
- 2Positive indices count from the beginning, while negative indices count backward from the end.
- 3Use
my_list[index] to retrieve an element andmy_list[index] = value to modify one element. - 4Lists are mutable, so an individual element can be changed without recreating the entire list.
- 5An invalid index raises
IndexError; check that the requested position exists before accessing it.
At a glance
- 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.
IndexErrormeans the requested index does not identify an existing element.