Quarry School

Update an item by index

Subject: Computing
Level 1 · FoundationsAbout 5 min read1 new term
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    A whiteboard of scores can be wiped and rewritten, one cell at a time, and a new row can be added at the bottom.

    Rule

    A list is mutable. a[i] = v replaces the item at index i with v and keeps the length; a.append(v) adds v at the end and makes the list one item longer. A string is immutable: assigning to one of its characters raises a TypeError.

    Step

    1. Write the list with each index under its item.

    2. Before name[i] = value, check that index i names an item: for a list of n items, from 0 up to n - 1, or from -1 back to -n counting from the end.

    3. For name[i] = value, cross out the item at index i and write the new value there.

    4. For name.append(value), write the value after the last item and add 1 to the length.

    5. Print the list to see its items after the changes, and print len() of it to see how many items it holds.

    Trap

    Assigning the result of append to the list's name loses that name for the list. append changes the list and gives back None, so after scores = scores.append(10) the list has grown, but scores names None.

    To add an item, use append, not an index one past the end: scores[3] = 10 on a list of three items stops with an IndexError.

    1 In plain words

    A whiteboard of scores can be wiped and rewritten, one cell at a time, and a new row can be added at the bottom.

    A list can be changed in the same way: it is mutable. Assigning to an index, as in scores[1] = 6, replaces the item at that index. The method append, as in scores.append(10), adds an item at the end, and the list grows by one.

    A string is not mutable. Assigning to one character of a string stops the program with a TypeError.

    Remember one sentence: an index assignment replaces one item, and append adds one at the end. To look it up, see sections 3.1.3 and 5.1 of the Python 3.13 tutorial, on lists.

    2 Picture it

    Linescores after itlen(scores)start[7, 4, 9]3scores[1] = 6[7, 6, 9]3append(10)[7, 6, 9, 10]4

    A table of the list scores after each line. At the start it holds 7, 4 and 9, three items. After scores[1] = 6 it holds 7, 6 and 9, still three. After scores.append(10) it holds 7, 6, 9 and 10, four items. The row for scores[1] = 6 is marked.

    The scores list after each line

    3 Why

    In scores[1] = 6 the left side names one place in the list. Python puts the new value there and the old value, 4, is gone. The other items and the length do not change.

    append changes the list itself and adds the item after the last one. It gives back None, so a line such as scores = scores.append(10) would leave scores naming None. Call it on a line of its own.

    Index assignment replaces an item that exists; it does not add one. scores[4] = 10 on a list of 4 items stops with an IndexError.

    4 The rule

    A list is mutable.

    a[i] = v replaces the item at index i with v and keeps the length; a.append(v) adds v at the end and makes the list one item longer. A string is immutable: assigning to one of its characters raises a TypeError.

    5 How

    1. Write the list with each index under its item.
    2. Before name[i] = value, check that index i names an item: for a list of n items, from 0 up to n - 1, or from -1 back to -n counting from the end.
    3. For name[i] = value, cross out the item at index i and write the new value there.
    4. For name.append(value), write the value after the last item and add 1 to the length.
    5. Print the list to see its items after the changes, and print len() of it to see how many items it holds.

    6 Worked example

    Replace an item and add one

    Python code
    scores = [7, 4, 9]
    scores[1] = 6
    scores.append(10)
    print(scores)
    print(len(scores))
    

    Standard input

    Empty

    Standard output

    [7, 6, 9, 10]
    4
    

    Predict the two output lines first, then run the file. The second score changes from 4 to 6, and 10 joins the end.

    Try to change one character of a string

    Python code
    code = "cat"
    code[0] = "b"
    

    Standard input

    Empty

    Standard output

    Empty

    Exception TypeError

    Save the two lines as code.py and run the file. A string cannot be changed, so the second line stops the program with a TypeError. Build a new string instead.

    Trace

    Trace the scores program
    LinescoresOutputExplanation
    scores = [7, 4, 9][7, 4, 9]—A list of three items.
    scores[1] = 6[7, 6, 9]—Index 1, the second item, is replaced.
    scores.append(10)[7, 6, 9, 10]—10 is added at the end.
    print(scores)[7, 6, 9, 10][7, 6, 9, 10]The list is printed in square brackets.
    print(len(scores))[7, 6, 9, 10]4The list now has four items.

    7 The trap

    Assigning the result of append to the list's name loses that name for the list. append changes the list and gives back None, so after scores = scores.append(10) the list has grown, but scores names None.

    To add an item, use append, not an index one past the end: scores[3] = 10 on a list of three items stops with an IndexError.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    marks = [3, 5, 8]
    marks[0] = 4
    marks.append(2)
    print(marks)
    Show the steps and the answer
    Answer

    It displays [4, 5, 8, 2].

    Why: Index 0 changes from 3 to 4, and append adds 2 at the end.

    Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.3 Lists

    Quick check 2 of 2

    A list row holds 5 items. After row[2] = 0, how many items does it hold? After row.append(0) as well?

    Show the steps and the answer
    Answer

    It holds 5 items after the assignment, and 6 after the append.

    Why: Index assignment replaces an item and keeps the length. append adds one item at the end.

    Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.3 Lists

    Sources

    1. Python 3.13 Tutorial, 3.1.3 Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Tutorial, 5.1 More on Lists. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Tutorial, 3.1.2 Text. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Library Reference, Built-in Exceptions: IndexError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.