← VCE Software Development U3O1

Runtime error: index out of range

A runtime error raised when code uses an index that is not in the list, such as items[5] in a 5-item list. The last valid index is len(items) - 1.

Understanding Runtime error: index out of range in VCE Software Development U3O1

What it does: The check makes sure i is between 0 and len(items) - 1 before the list is accessed, so no IndexError is raised. An empty list has no valid indexes at all, so even index 0 is out of range. The first index is 0, so the last valid index is one less than the length. Using len(items) as an index goes past the end and causes an IndexError. A list with 5 items has indexes 0 to 4, so len(items) is 5 and is one past the end.

Common mistakes: The list has 5 items, so the last valid index is 4 (len(scores) - 1). Using last + 1 gives index 5, which raises an IndexError. It should be print(scores[last]). This is an off-by-one error. range(count + 1) makes i reach 3, but the valid indexes are 0 to 2, so names[3] raises an IndexError. It should be range(count).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A list has 5 items: scores = [4, 8, 6, 9, 7]. Which statement is true about scores[5]?
It raises an IndexError because the last valid index is 4It returns 7 because it is the fifth item
Matches
for i in range(len(items) + 1): print(items[i])
↓
Off-by-one error: the loop goes one step too far, so the last value of i equals len(items) and raises an IndexError. Use range(len(items)).
Applying
Meanings
Term
IndexError
Meaning
A runtime error raised when code uses an index that is not in the list, such as items[5] in a 5-item list. The last valid index is len(items) - 1.
Practice
scores = [72, 85, 90, 64, 78]
Testing
Gaps
last = items[len(items) - ]
Check Answer
Spots
2last = len(scores) - 1
3print(scores[last + 1])

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Squirrel' badge — Module Hoarder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A list has 5 items: scores = [4, 8, 6, 9, 7]. Which statement is true about scores[5]?
A. It raises an IndexError because the last valid index is 4 ✓
B. It returns 7 because it is the fifth item
Python counts from 0, so a 5-item list has indexes 0 to 4. Index 5 is outside the list, so Python raises an IndexError.
2. Which expression gives the last valid index of a list called items?
A. len(items) - 1 ✓
B. len(items)
The first index is 0, so the last valid index is one less than the length. Using len(items) as an index goes past the end and causes an IndexError.
3. Which loop header causes an off-by-one IndexError when reading names[i]?
A. for i in range(len(names) + 1): ✓
B. for i in range(len(names)):
range(len(names) + 1) produces one index too many. On the final pass i equals len(names), which is not a valid index, so an IndexError is raised.
4. Given names = [], what happens when the code runs print(names[0])?
A. It prints None because the list is empty
B. An IndexError is raised because an empty list has no index 0 ✓
An empty list has no valid indexes at all. Even index 0 is out of range, so Python raises an IndexError rather than returning None.
5. Which check helps prevent an IndexError before using items[i]?
A. if i <= len(items):
B. if i >= 0 and i < len(items): ✓
A range check must confirm the index is from 0 up to len(items) - 1. Using <= len(items) still allows i to equal len(items), which is out of range.

Match the Pairs

items[len(items)] → Raises an IndexError, because the last valid index is len(items) - 1
items[len(items) - 1] → Gets the last item in the list safely, as long as the list is not empty
Prevent an IndexError with a range check before using the index → if 0 <= i < len(items): print(items[i])
Accessing the first item of an empty list causes an IndexError → items = [] print(items[0])
for i in range(len(items) + 1): print(items[i]) → Off-by-one error: the loop goes one step too far, so the last value of i equals len(items) and raises an IndexError. Use range(len(items)).
scores = [70, 85, 90] print(scores[3]) → A 3-item list only has indexes 0, 1 and 2, so index 3 is out of range. The last item is scores[2] or scores[-1].

Key Term

IndexError
A runtime error raised when code uses an index that is not in the list, such as items[5] in a 5-item list. The last valid index is len(items) - 1.

Fill in the Blanks

1. items = [4, 8, 15, 16, 23] last = items[len(items) - 1] print(last)
Hint: Type the number subtracted from the length to get the last valid index
2. items = ["a", "b", "c"] for i in range(len(items)): print(items[i])
Hint: Type the function that returns how many items are in the list, so the loop stops before an invalid index
3. items = [] if len(items) > 0: print(items[0])
Hint: Type the comparison operator that checks the list is not empty before accessing index 0
4. try: print(items[5]) except IndexError:
Hint: Type the name of the error raised when an index is not in the list
5. if index >= 0 and index < len(items): print(items[index])
Hint: Type the comparison operator that keeps the index below the list length

Spot the Error

Question 1
A scores = [4, 8, 6, 9, 7]
B last = len(scores) - 1
C print(scores[last + 1])
Line C — The list has 5 items, so the last valid index is 4 (len(scores) - 1). Using last + 1 gives index 5, which raises an IndexError. It should be print(scores[last]).
Question 2
A names = ['Ava', 'Zoe', 'Leo']
B count = len(names)
C print('Names:', count)
D for i in range(count + 1):
E print(names[i])
Line D — This is an off-by-one error. range(count + 1) makes i reach 3, but the valid indexes are 0 to 2, so names[3] raises an IndexError. It should be range(count).
Question 3
A items = [10, 20, 30, 40]
B total = 0
C i = 0
D while i <= len(items):
E total += items[i]
F i += 1
Line D — With <=, the loop runs when i equals 4, but the last valid index is 3. items[4] raises an IndexError. The condition should be i < len(items).
Question 4
A prices = [3, 5, 8]
B last = prices[len(prices)]
C print(last)
Line B — len(prices) is 3, but indexes only go up to 2. The last item is at len(prices) - 1. Without the - 1, the code raises an IndexError.
Question 5
A colours = ['red']
B print(colours[0])
C print(colours[1])
Line A — With only one item, the only valid index is 0. Then colours[1] on line 3 raises an IndexError. The list needs two items, 'red' and 'blue'.

Student with a class code? Join your class →