List Methods for Adding and Removing Elements
The same list can be viewed from the end: index -1 refers to "yellow", index -2 to "blue", index -3 to "green", and index -4 to "red".
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 One Element
A list can contain several values, but many tasks require one specific element. Python uses an index to identify an element's position. Positive indices count from the beginning of the list, while negative indices count from the end. The bracket operator retrieves the value at a chosen index.
What do you think happens?
Suppose a list contains four elements. Which position does index 0 identify?
Reveal answer
The first position
Python list indices start at 0, so index 0 identifies the first element rather than representing a count of one completed position.
Positive and Negative Positions
Consider the generated list ["red", "green", "blue", "yellow"]. From the beginning, index 0 refers to "red", index 1 to "green", index 2 to "blue", and index 3 to "yellow". The same list can be viewed from the end: index -1 refers to "yellow", index -2 to "blue", index -3 to "green", and index -4 to "red". The positive and negative forms identify positions from opposite directions.
| Index | Direction | Element in the generated list |
|---|---|---|
| 0 | From the beginning | red |
| 1 | From the beginning | green |
| 2 | From the beginning | blue |
| 3 | From the beginning | yellow |
| -1 | From the end | yellow |
| -2 | From the end | blue |
| -3 | From the end | green |
| -4 | From the end | red |
The same four list elements can be identified by positive or negative indices.
Retrieving and Reassigning Values
Replacing One List Element
A generated list contains the values red, green, blue, and yellow. Retrieve the element at index 2, then replace the value at that same index with purple.
- 1Locate the position
Starting at index 0, the element at index 2 is blue.
- 2Retrieve the value
Using the bracket operator with index 2 retrieves blue.
- 3Assign a replacement
Assigning purple at index 2 changes that individual list element.
- 4Check the negative index
Because index -2 identifies the same position in this four-element list, the replacement can also be made using index -2.
The list changes from [red, green, blue, yellow] to [red, green, purple, yellow].
The bracket operator has two related uses. Reading a bracket expression retrieves a value at an index. Assigning a value to a bracket expression modifies the element at that index. Lists are mutable, so an individual element can be changed without recreating the entire list.
Invalid Indexes
An IndexError occurs when an index does not exist in the list. To debug the problem, inspect the index being used and compare it with the list positions available from the beginning or from the end. An index that points beyond those positions cannot retrieve or modify an element.
A generated list has four elements, with positions identified by positive indices 0 through 3 and negative indices -1 through -4. Attempting to use index 4 or index -5 refers to a position that does not exist, so the access produces an IndexError. The correction is to verify the intended position and choose an index that identifies an existing element.
Frequent Indexing Mistakes
Common mistake Treating index 1 as the first element
What it looks likeUsing index 1 when you intend to retrieve the first position
Why it failsList indices start at 0, so index 1 identifies the second position.
Do this insteadUse index 0 for the first position.
Common mistake Counting a negative index from the beginning
What it looks likeExpecting index -1 to identify the first element
Why it failsNegative indices count from the end, and -1 identifies the last position.
Do this insteadCount backward from the last element when using a negative index.
Common mistake Assuming an index can identify any position
What it looks likeTrying to access a position that is not present in the list
Why it failsAn index that does not exist causes an
IndexError.Do this insteadVerify that the index identifies a valid position before retrieving or modifying the element.
Common mistake Confusing retrieval with modification
What it looks likeExpecting a bracket expression used for access to change the list
Why it failsReading an indexed value retrieves it; modification requires assigning a new value at the index.
Do this insteadUse assignment at the chosen index when you need to replace an element.
When an indexed operation fails, do not change the index randomly. First identify whether you intended to count from the beginning or from the end, then check whether the selected position exists. This separates an indexing-direction mistake from an out-of-range mistake.
Practice Check
For the generated list ["oak", "pine", "maple", "cedar"], identify the element at index 0, the element at index -1, and the element at index -3. Then describe the resulting list after replacing the element at index 1 with "birch".
Hints
- Start positive counting at 0.
- Start negative counting at the last element with -1.
- Reassignment changes the value at the selected position.
Practice Solution
Solve the indexing task for [oak, pine, maple, cedar].
- 1Index 0
Index 0 identifies oak because positive indices begin at the first position.
- 2Index -1
Index -1 identifies cedar because negative indices begin at the last position.
- 3Index -3
Counting backward gives maple at index -2 and pine at index -3.
- 4Reassign index 1
Replacing the value at index 1 changes pine to birch.
The resulting list is [oak, birch, maple, cedar].
Key Takeaways
- 1List indices start at 0, so index 0 identifies the first element.
- 2Positive indices count from the beginning, while negative indices count from the end.
- 3The bracket operator retrieves an element when used for access.
- 4Assigning a value at an index modifies that individual element because lists are mutable.
- 5An
IndexErrormeans the requested index does not exist; inspect the direction and range of the index.
At a glance
- Use indices to identify individual list elements.
- Index 0 is the first position, and negative indices count backward from the end.
- Assign a new value at an index to modify one list element.
- Check the chosen index when an
IndexErroroccurs.