Modifying List Elements
List indexing uses the bracket operator with zero-based indices: the first element is at index 0, the second at index 1, and so on.
Finding a Position
A list stores elements in an ordered sequence. To retrieve one element, write the list name followed by brackets containing an index. The index identifies the element's position, and Python starts counting those positions at zero rather than one.
The first element is at index 0, the second is at index 1, and each later element increases the index by one.
Zero-Based List Positions
Selecting the Third Element
A list has the ordered elements Cheddar, Gouda, Mozzarella, and Brie. Which element is selected by index 2?
Start counting at zero: The first element, Cheddar, has index 0. The second element, Gouda, has index 1.
Continue to index 2: The third element is therefore at index 2.
Retrieve the element: Using the list name with index 2 selects Mozzarella.
Index 2 selects the third element, Mozzarella.
Evaluating an Index
The expression inside the brackets does not have to be a literal number. Any integer expression can be used as an index. A variable, an arithmetic expression, or a function call can supply the index as long as its result is an integer.
Resolving an Arithmetic Index
Suppose index has the integer value 1. Which element is selected by cheeses[index + 1]?
Evaluate the expression: The expression index + 1 becomes 1 + 1, which evaluates to 2.
Use the resulting index: The list access is therefore equivalent to selecting cheeses[2].
Identify the element: In the source example, index 2 selects the third element, Mozzarella.
The expression selects the element at index 2, which is Mozzarella.
When reading a bracket expression, first evaluate the expression inside the brackets. Then use the resulting integer as the position in the list.
Counting from the End
Negative indices provide another way to locate list elements. Instead of counting forward from the beginning, they count backward from the end. Index -1 identifies the last element, -2 identifies the second-to-last element, and the pattern continues toward the beginning.
Reading from the Right
For the four-element list Cheddar, Gouda, Mozzarella, and Brie, identify the elements selected by indices -1, -2, and -4.
Use -1: Counting backward one position from the end selects Brie, the last element.
Use -2: Counting backward two positions selects Mozzarella, the second-to-last element.
Use -4: Counting backward four positions reaches Cheddar, the first element.
-1 selects Brie, -2 selects Mozzarella, and -4 selects Cheddar.
Replacing a List Element
Modifying a list element means working with a particular position and assigning that position a new value. The position is still identified through indexing, so zero-based and negative indexing determine which element is targeted. The other positions remain separate from the selected position.
When an Index Is Invalid
Every list has a valid range of indices. For a list with four elements, the non-negative indices are 0, 1, 2, and 3. The corresponding negative indices are -1, -2, -3, and -4. Requesting an index outside either range attempts to access an element that does not exist and raises IndexError.
Checking the Boundaries
A list contains four elements. What happens when an access requests index 4 or index -5?
Check index 4: The valid non-negative indices stop at 3, so index 4 is outside the list.
Check index -5: The valid negative indices stop at -4, so index -5 is also outside the list.
Identify the result: Both requests refer to positions that do not exist and raise IndexError.
Both index 4 and index -5 cause IndexError for a four-element list.
Before accessing or modifying an element, determine the list length and confirm that the requested index lies within the valid positive or negative range. This prevents an access to a non-existent element.
Common Indexing Mistakes
Treating the first element as index 1
The first element is at index 0, so index 1 selects the second element.
Fix:
Start the position count at zero.Assuming the last positive index equals the number of elements
A four-element list has valid positive indices 0 through 3.
Fix:
Use the highest valid index, which is one less than the number of elements.Counting negative indices from zero
The last element is at index -1, while index 0 refers to the first element.
Fix:
Begin backward counting at -1.Forgetting to evaluate an expression inside brackets
The resulting integer determines the actual list position.
Fix:
Evaluate the expression first, then use that result as the index.
Position Practice
Consider a four-element list ordered as Cheddar, Gouda, Mozzarella, and Brie. Identify the element selected by each index: 0, 2, -1, and -4. Then decide whether index 4 and index -5 identify an element or raise IndexError.
Hints
- Write the non-negative positions from 0 through 3.
- Write the negative positions from -1 through -4, beginning at the last element.
- Compare 4 and -5 with the valid ranges before deciding.
What do you think happens?
For a four-element list, what happens when the requested index is -5?
Reveal answer
Answer: IndexError is raised.
For a four-element list, the valid negative indices range from -1 to -4. Index -5 is beyond that range.
Key Takeaways
- Use the bracket operator to access a list element by position.
- List positions are zero-based: the first element is at index 0.
- Any integer expression can provide an index after it is evaluated.
- Negative indices count backward, with -1 identifying the last element.
- An index outside the valid range raises IndexError.
Key Takeaways
- Use brackets with a zero-based integer index to locate a list element.
- Evaluate variables and arithmetic expressions inside brackets before using their integer result as the index.
- Use negative indices to count backward from the end of a list.
- For a four-element list, valid indices range from 0 to 3 and from -1 to -4.
- Requesting an index outside the valid range raises IndexError.