Modifying List Elements
The first element is at index 0, the second is at index 1, and each later element increases the index by one.
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…
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?
- 1Start counting at zero
The first element, Cheddar, has index 0. The second element, Gouda, has index 1.
- 2Continue to index 2
The third element is therefore at index 2.
- 3Retrieve 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]?
- 1Evaluate the expression
The expression index + 1 becomes 1 + 1, which evaluates to 2.
- 2Use the resulting index
The list access is therefore equivalent to selecting cheeses[2].
- 3Identify 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.
- 1Use -1
Counting backward one position from the end selects Brie, the last element.
- 2Use -2
Counting backward two positions selects Mozzarella, the second-to-last element.
- 3Use -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?
- 1Check index 4
The valid non-negative indices stop at 3, so index 4 is outside the list.
- 2Check index -5
The valid negative indices stop at -4, so index -5 is also outside the list.
- 3Identify 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
Common mistake Treating the first element as index 1
What it looks likeUsing index 1 when the goal is to retrieve the first element.
Why it failsThe first element is at index 0, so index 1 selects the second element.
Do this insteadStart the position count at zero.
Common mistake Assuming the last positive index equals the number of elements
What it looks likeUsing index 4 for a list with four elements.
Why it failsA four-element list has valid positive indices 0 through 3.
Do this insteadUse the highest valid index, which is one less than the number of elements.
Common mistake Counting negative indices from zero
What it looks likeExpecting negative index 0 to mean the last element.
Why it failsThe last element is at index -1, while index 0 refers to the first element.
Do this insteadBegin backward counting at -1.
Common mistake Forgetting to evaluate an expression inside brackets
What it looks likeReading index + 1 as a label instead of calculating its integer result.
Why it failsThe resulting integer determines the actual list position.
Do this insteadEvaluate 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
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
- 1Use the bracket operator to access a list element by position.
- 2List positions are zero-based: the first element is at index 0.
- 3Any integer expression can provide an index after it is evaluated.
- 4Negative indices count backward, with -1 identifying the last element.
- 5An index outside the valid range raises
IndexError.
At a glance
- 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.