Quarry School

Build an ordered list

Subject: Computing
Level 1 · FoundationsAbout 5 min read3 new terms
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.

    Leans on

    Quick review (2 minutes)

    In plain words

    A queue at a counter has a first person, a second and so on, and each person keeps a place in line.

    Rule

    A list is written as values between square brackets, separated by commas, and keeps them in order. a[i] gives the item at index i, counted from 0; a[-1] gives the last item; len(a) gives the number of items. An index from len(a) up raises an IndexError.

    Step

    1. Write the items in order between square brackets, separated by commas.

    2. Number the items from 0 under the list as you trace it.

    3. Before reading an item, check its index against the length: in a list that is not empty, the largest index is one less than len(), and an empty list has no item to read.

    4. To read an item, use its number in square brackets after the list's name. Use -1 for the last item.

    5. To work through every item, write a for loop over the list.

    Trap

    The usual slip is counting from 1. In a list of 3 items, queue[3] is not the last item: it is past the end, and the program stops with an IndexError. Use queue[2], or queue[-1].

    Square brackets do two jobs here. In ["Ada", "Ben"] they make a list; after a name, as in queue[0], they read an item.

    1 In plain words

    A queue at a counter has a first person, a second and so on, and each person keeps a place in line.

    A list keeps values in order in the same way. Write it as values separated by commas between square brackets, such as ["Ada", "Ben", "Cy"]. Each value is an item of the list.

    Items are found by their index, a position counted from 0. In queue = ["Ada", "Ben", "Cy"], queue[0] is "Ada" and queue[2] is "Cy". An index of -1 counts from the end and gives the last item. len(queue) gives the number of items, 3.

    Remember one sentence: a list keeps its items in order, numbered from 0. To look it up, see section 3.1.3 of the Python 3.13 tutorial, on lists.

    2 Picture it

    IndexItemAlso0Adafirst1Ben2Cyindex -1

    A table of the list queue. Index 0 holds Ada, the first item. Index 1 holds Ben. Index 2 holds Cy, which index -1 also gives. The row for index 0 is marked.

    The three items of the queue list and their indexes

    3 Why

    Counting from 0 means the first item is 0 steps from the start. The last item of a list of 3 is then at index 2, one less than the length.

    An index of 3 in a list of 3 items points past the end. Python stops the program with an IndexError, the error for a sequence index out of range.

    A list is a sequence, like a string, so a for loop works through its items in order. Printing a whole list shows its items in square brackets, with quotation marks around text items.

    4 The rule

    A list is written as values between square brackets, separated by commas, and keeps them in order.

    a[i] gives the item at index i, counted from 0; a[-1] gives the last item; len(a) gives the number of items. An index from len(a) up raises an IndexError.

    5 How

    1. Write the items in order between square brackets, separated by commas.
    2. Number the items from 0 under the list as you trace it.
    3. Before reading an item, check its index against the length: in a list that is not empty, the largest index is one less than len(), and an empty list has no item to read.
    4. To read an item, use its number in square brackets after the list's name. Use -1 for the last item.
    5. To work through every item, write a for loop over the list.

    6 Worked example

    Read a list by index and by loop

    Python code
    queue = ["Ada", "Ben", "Cy"]
    print(queue[0])
    print(queue[-1])
    print(len(queue))
    for name in queue:
        print(f"Next: {name}")
    

    Standard input

    Empty

    Standard output

    Ada
    Cy
    3
    Next: Ada
    Next: Ben
    Next: Cy
    

    Predict the six output lines first. Then save the code as queue.py and run the file. queue[0] is the first item, queue[-1] the last, and the loop visits the items in order.

    Ask for an index past the end

    Python code
    queue = ["Ada", "Ben", "Cy"]
    print(queue[3])
    

    Standard input

    Empty

    Standard output

    Empty

    Exception IndexError

    Save the two lines as end.py and run the file. The list has items at 0, 1 and 2, so index 3 is out of range and the program stops with an IndexError.

    Trace

    Trace the queue program
    LineValueOutputExplanation
    print(queue[0])"Ada"AdaIndex 0 is the first item.
    print(queue[-1])"Cy"CyIndex -1 is the last item.
    print(len(queue))33The list has three items.
    the for loopeach name in turnNext: Ada, Next: Ben, Next: CyThe loop name takes each item in order.

    7 The trap

    The usual slip is counting from 1. In a list of 3 items, queue[3] is not the last item: it is past the end, and the program stops with an IndexError. Use queue[2], or queue[-1].

    Square brackets do two jobs here. In ["Ada", "Ben"] they make a list; after a name, as in queue[0], they read an item.

    Quick checks

    Quick check 1 of 2

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

    Python code
    colors = ["red", "green", "blue", "gold"]
    print(colors[1])
    print(colors[-1])
    print(len(colors))
    Show the steps and the answer
    Answer

    It displays green, gold and 4, each on its own line.

    Why: Index 1 is the second item, green. Index -1 is the last item, gold. The list has four items.

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

    Quick check 2 of 2

    A list days holds 7 items. What is the largest index you can use with it, counting from the start?

    Show the steps and the answer
    Answer

    The largest index is 6.

    Why: Counting from 0, the indexes from the start run from 0 to one less than the length, so 0 to 6.

    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 Library Reference, Built-in Exceptions: IndexError. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.