Concepts / Modifying List Elements

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.

  • Programming
Interactive lab

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

  1. Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
  2. An index outside the string raises IndexError.
  3. A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
  4. 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.

Educational simulation

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.

Key point

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

0Cheddar1Gouda2Mozzarella3Brie
Which list element does each index point to, and how does index 0 differ from index 1?
Worked example

Selecting the Third Element

A list has the ordered elements Cheddar, Gouda, Mozzarella, and Brie. Which element is selected by index 2?

  1. 1
    Start counting at zero

    The first element, Cheddar, has index 0. The second element, Gouda, has index 1.

  2. 2
    Continue to index 2

    The third element is therefore at index 2.

  3. 3
    Retrieve the element

    Using the list name with index 2 selects Mozzarella.

Result

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.

Worked example

Resolving an Arithmetic Index

Suppose index has the integer value 1. Which element is selected by cheeses[index + 1]?

  1. 1
    Evaluate the expression

    The expression index + 1 becomes 1 + 1, which evaluates to 2.

  2. 2
    Use the resulting index

    The list access is therefore equivalent to selecting cheeses[2].

  3. 3
    Identify the element

    In the source example, index 2 selects the third element, Mozzarella.

Result

The expression selects the element at index 2, which is Mozzarella.

Key point

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.

-4Cheddar-3Gouda-2Mozzarella-1Brie
How do negative indices map positions counting backward from the last element?
Worked example

Reading from the Right

For the four-element list Cheddar, Gouda, Mozzarella, and Brie, identify the elements selected by indices -1, -2, and -4.

  1. 1
    Use -1

    Counting backward one position from the end selects Brie, the last element.

  2. 2
    Use -2

    Counting backward two positions selects Mozzarella, the second-to-last element.

  3. 3
    Use -4

    Counting backward four positions reaches Cheddar, the first element.

Result

-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.

Cheddarindex 0Cheddarindex 0Goudaindex 1Parmesanindex 1Mozzarellaindex 2Mozzarellaindex 2new value
What changes in the list when an element at a specific index is assigned a new value?

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.

Requested indexValid range0 to 3 or -1 to -4List elementIndexErrorindex does not existcheckyesno
What happens when the requested index is outside the list's valid range?
Worked example

Checking the Boundaries

A list contains four elements. What happens when an access requests index 4 or index -5?

  1. 1
    Check index 4

    The valid non-negative indices stop at 3, so index 4 is outside the list.

  2. 2
    Check index -5

    The valid negative indices stop at -4, so index -5 is also outside the list.

  3. 3
    Identify the result

    Both requests refer to positions that do not exist and raise IndexError.

Result

Both index 4 and index -5 cause IndexError for a four-element list.

Best practice

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 like

    Using index 1 when the goal is to retrieve the first element.

    Why it fails

    The first element is at index 0, so index 1 selects the second element.

    Do this instead

    Start the position count at zero.

  • Common mistake

    Assuming the last positive index equals the number of elements

    What it looks like

    Using index 4 for a list with four elements.

    Why it fails

    A four-element list has valid positive indices 0 through 3.

    Do this instead

    Use the highest valid index, which is one less than the number of elements.

  • Common mistake

    Counting negative indices from zero

    What it looks like

    Expecting negative index 0 to mean the last element.

    Why it fails

    The last element is at index -1, while index 0 refers to the first element.

    Do this instead

    Begin backward counting at -1.

  • Common mistake

    Forgetting to evaluate an expression inside brackets

    What it looks like

    Reading index + 1 as a label instead of calculating its integer result.

    Why it fails

    The resulting integer determines the actual list position.

    Do this instead

    Evaluate the expression first, then use that result as the index.

Position Practice

EASY

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?

  • The first element is selected

  • The last element is selected

  • IndexError is raised

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

  1. 1
    Use the bracket operator to access a list element by position.
  2. 2
    List positions are zero-based: the first element is at index 0.
  3. 3
    Any integer expression can provide an index after it is evaluated.
  4. 4
    Negative indices count backward, with -1 identifying the last element.
  5. 5
    An 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.

Related concepts

More concepts starting with M