Understanding Lists and Their Properties
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 the First Element
A list stores elements in an ordered sequence. To retrieve one element, write the list name followed by brackets containing an index. The important starting point is that Python counts list positions from zero: the first element has index 0, the second has index 1, and later elements continue in the same pattern.
Reading Positions with Brackets
The bracket operator selects an element by position. For the source example, cheeses[0] retrieves the first element, and cheeses[2] retrieves the third element. The number inside the brackets is not a human-style position number; it is a zero-based index. Therefore, an index of 2 means the third element because counting begins at 0.
Selecting a Known Element
Determine which element is selected by cheeses[2].
Start at zero: The first element is at index 0, so the indices advance as 0, 1, and 2.
Locate index 2: Index 2 identifies the third element in the list.
Retrieve the element: cheeses[2] retrieves the third element.
The expression selects the third element of cheeses.
Evaluating an Index Expression
The index does not have to be a literal integer written directly inside the brackets. Any integer expression can be used. This includes a variable, an arithmetic expression, or a function call that evaluates to an integer. Python evaluates that expression first, then uses the resulting integer to select the list element.
Using a Variable and Arithmetic
Determine the index used by cheeses[index + 1] when index is 1.
Read the variable: The variable index has the integer value 1.
Evaluate the expression: The expression index + 1 becomes 1 + 1, which evaluates to 2.
Use the result: The list access therefore uses index 2.
cheeses[index + 1] retrieves the element at index 2, which is the third element.
Counting Backward
Negative indices provide a second way to locate elements. They count backward from the end of the list: -1 selects the last element, -2 selects the second-to-last element, and the count continues toward the beginning. In the source example, cheeses[-1] selects Mozzarella, cheeses[-2] selects Gouda, and cheeses[-4] selects Cheddar.
Reading from the End
Identify the elements selected by cheeses[-1], cheeses[-2], and cheeses[-4].
Use -1: -1 means the last element, which is Mozzarella in the source example.
Use -2: -2 means the second-to-last element, which is Gouda.
Use -4: The list has four elements, so -4 reaches the first element, Cheddar.
The expressions select Mozzarella, Gouda, and Cheddar respectively.
When an Index Fails
A list can only be accessed through its valid index range. The source example has four elements, so its non-negative indices are 0, 1, 2, and 3. Attempting to access index 4 goes beyond the available elements and raises an IndexError. The same problem occurs when an index is too negative: cheeses[-5] is invalid for a four-element list because it goes beyond the beginning.
Treating index 1 as the first element
List indices begin at 0, so index 1 identifies the second element.
Fix:
Use index 0 for the first element, index 1 for the second, and index 2 for the third.Assuming the last index equals the number of elements
A four-element list uses non-negative indices 0 through 3. Index 4 is outside the valid range and raises IndexError.
Fix:
For a four-element list, the final non-negative index is 3.Counting too far backward
The valid negative indices for that list range from -1 through -4.
Fix:
Keep the negative index within the available backward range.
Practice and Review
For the source list cheeses, identify the selected position for cheeses[0], cheeses[2], cheeses[index] when index is 1, cheeses[index + 1] when index is 1, cheeses[-1], and cheeses[-2]. Then determine which two accesses would raise IndexError: cheeses[4] or cheeses[-5].
Hints
- Translate non-negative indices from zero: 0 is first, 1 is second, and 2 is third.
- Evaluate index + 1 before using it as the list index.
- For a four-element list, the valid negative indices range from -1 through -4.
- List elements are retrieved with the bracket operator. Python uses zero-based indexing, so index 0 identifies the first element. The expression inside the brackets can be any expression that evaluates to an integer, including a variable or arithmetic expression. Negative indices count backward from the end, with -1 selecting the last element. An index outside the valid range raises IndexError.
Key Takeaways
- Use brackets to retrieve a list element by index.
- The first element is at index 0, not index 1.
- Integer expressions are evaluated before their results are used as indices.
- Negative indices count backward, with -1 selecting the last element.
- An index outside the available range raises IndexError.