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.
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 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].
- 1Start at zero
The first element is at index 0, so the indices advance as 0, 1, and 2.
- 2Locate index 2
Index 2 identifies the third element in the list.
- 3Retrieve 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.
- 1Read the variable
The variable index has the integer value 1.
- 2Evaluate the expression
The expression index + 1 becomes 1 + 1, which evaluates to 2.
- 3Use 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].
- 1Use -1
-1 means the last element, which is Mozzarella in the source example.
- 2Use -2
-2 means the second-to-last element, which is Gouda.
- 3Use -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.
Common mistake Treating index 1 as the first element
What it looks likeUsing index 1 when the intended element is the first item
Why it failsList indices begin at 0, so index 1 identifies the second element.
Do this insteadUse index 0 for the first element, index 1 for the second, and index 2 for the third.
Common mistake Assuming the last index equals the number of elements
What it looks likeAccessing index 4 in a list with four elements
Why it failsA four-element list uses non-negative indices 0 through 3. Index 4 is outside the valid range and raises
IndexError.Do this insteadFor a four-element list, the final non-negative index is 3.
Common mistake Counting too far backward
What it looks likeUsing cheeses[-5] for the four-element source list
Why it failsThe valid negative indices for that list range from -1 through -4.
Do this insteadKeep 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.
- 1List 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.