Concepts / Understanding Lists as Ordered Collections

Understanding Lists as Ordered Collections

Lists are mutable, which means individual elements can be changed without recreating the entire list.

  • Programming

Finding One Item

A list keeps values in an order, so you can ask for a particular element by referring to its index. The bracket operator connects the list with that index: my_list[index] retrieves a value, while my_list[index] = value changes the value stored at that position. The main challenge is remembering how Python numbers positions from the beginning and from the end.

Mapping Positions to Indices

Consider the generated list ["red", "green", "blue"]. From the beginning, the first element has index 0, the second has index 1, and the third has index 2. Python also lets you count backward: -1 refers to the last element, -2 to the element before it, and -3 to the first element. Positive and negative indices can therefore identify the same element from different directions.

red0 and -3green1 and -2blue2 and -1next positionnext position
Which element does each positive or negative index refer to when counting from the start versus the end?
Key point

The first element is index 0, not index 1. The last element can be reached with a positive index from the beginning or with index -1 from the end.

Reading an Element

Worked example

Reading from Both Directions

Given the generated list ["Monday", "Tuesday", "Wednesday"], determine which value each expression retrieves: days[0], days[2], days[-1], and days[-3].

  1. 1
    days[0]

    Index 0 identifies the first element, so this expression retrieves Monday.

  2. 2
    days[2]

    Index 2 identifies the third element when counting from the beginning, so this expression retrieves Wednesday.

  3. 3
    days[-1]

    Index -1 identifies the last element when counting backward, so this expression also retrieves Wednesday.

  4. 4
    days[-3]

    Index -3 identifies the third element counted backward in this three-element list, so this expression retrieves Monday.

Result

The positive and negative forms can point to the same element: days[0] and days[-3] identify Monday, while days[2] and days[-1] identify Wednesday.

python
Output
first_day is "Monday" and last_day is "Wednesday".

Changing One Position

Lists are mutable, which means individual elements can be changed without recreating the entire list. Put the target index on the left side of an assignment and the replacement value on the right side. Only the element at that index is reassigned; the other list elements remain in their existing positions.

Before
colors = ["red", "green", "blue"]
colors[1] = "yellow"

The value at index 1 is "green".

After
colors = ["red", "yellow", "blue"]

The value at index 1 is "yellow".

[red, green, blue][red, yellow, blue]index 1 reassigned
What changes in the list after assigning a new value to index 1, and which values stay the same?
Key point

Accessing and modifying use similar bracket notation, but their roles differ: my_list[index] reads a value, while my_list[index] = value replaces the value at that position.

Handling Invalid Indices

An index is valid only when it identifies an element that exists in the list. If an access expression refers to an index that does not exist, Python raises an IndexError instead of returning a value. Debugging begins by checking the list's actual elements and then checking whether the chosen index counts to an existing position from the beginning or from the end.

listtarget collectionindexrequested positionvalid index?element valueIndexErrorapply accesscheck positionyesno
What happens when an index is outside the valid range, and how does Python decide whether to return an element or raise IndexError?
python
Output
The first access retrieves "cat". The second access raises IndexError because index 2 does not identify an element in this list.

Mistakes with List Positions

  • Common mistake

    Treating the first element as index 1

    What it looks like
    items = ["a", "b"]; items[1]
    Why it fails

    Indices start at 0, so index 1 refers to the second element rather than the first.

    Do this instead

    Use items[0] when you need the first element.

  • Common mistake

    Forgetting that negative indices count from the end

    What it looks like
    items[-1]
    Why it fails

    The expression does not refer to the first element; -1 refers to the last element.

    Do this instead

    Use a non-negative index for counting from the beginning, or use -1 deliberately when you need the last element.

  • Common mistake

    Using an index that does not exist

    What it looks like
    items = ["a", "b"]; items[2]
    Why it fails

    Index 2 does not identify an element in this two-element generated list, so the access raises IndexError.

    Do this instead

    Verify that the requested index identifies an existing position before accessing the list.

  • Common mistake

    Assigning to the list name instead of an indexed position

    What it looks like
    items = "new value"
    Why it fails

    This assignment does not target one element at a specific index.

    Do this instead

    Use items[index] = value when the goal is to modify one list element.

Practice the Trace

EASY

For the generated list numbers = ["one", "two", "three", "four"], identify the value retrieved by each expression: numbers[0], numbers[3], numbers[-1], and numbers[-4]. Then determine the list after this assignment: numbers[1] = "TWO". Finally, identify which expression would raise IndexError: numbers[4] or numbers[-4].

Hints
  • Write the positive indices below the list from left to right, beginning with 0.
  • Write the negative indices below the list from right to left, beginning with -1.
  • For the assignment, change only the element at index 1.
  • Check whether each requested index identifies an existing element.
Worked example

Practice Check

Use numbers = ["one", "two", "three", "four"] to verify the four accesses, the reassignment, and the invalid-index check.

  1. 1
    Accesses

    numbers[0] is one, numbers[3] is four, numbers[-1] is four, and numbers[-4] is one.

  2. 2
    Reassignment

    Changing numbers[1] replaces two with TWO while the other elements stay in their positions.

  3. 3
    Invalid access

    numbers[4] raises IndexError because index 4 is outside the available positions. numbers[-4] is valid because it identifies the first element.

Result

After the reassignment, the list is ["one", "TWO", "three", "four"].

Essential Takeaways

  1. 1
    List indices begin at 0, so index 0 identifies the first element.
  2. 2
    Positive indices count from the beginning, while negative indices count backward from the end.
  3. 3
    Use my_list[index] to retrieve an element and my_list[index] = value to modify one element.
  4. 4
    Lists are mutable, so an individual element can be changed without recreating the entire list.
  5. 5
    An invalid index raises IndexError; check that the requested position exists before accessing it.

At a glance

  • A list's first element is at index 0.
  • Positive indices count from the beginning, and negative indices count from the end.
  • Bracket notation retrieves an element and can also target an element for reassignment.
  • Lists are mutable, so one indexed value can change while the rest of the list stays in place.
  • IndexError means the requested index does not identify an existing element.

More concepts starting with U