← ACARA Digital Technologies Years 9–10

Edge cases with lists and strings

An unusual but allowed input, such as an empty list, a one-item list, repeated values, or the maximum at the start or end, that you test because it can break code.

Understanding Edge cases with lists and strings in ACARA Digital Technologies Years 9–10

What it does: With one item, index 0 and index -1 point to the same value, so loops that compare items may never run. An empty list is allowed input but unusual. Code like max_val = nums[0] crashes on it, so test it. The maximum 9 appears twice, so you must decide which position is correct (usually the first) and test it. An empty string has no first character, so name[0] causes an IndexError. An empty string is the string version of an empty list. Typical data often passes even when the code is faulty. Edge cases are where bugs hide. Repeated values test whether code that finds a maximum or sorts still works when no item is bigger than the others.

Common mistakes: For an empty list there is no item at index 0, so nums[0] raises an IndexError. The empty-list case should return a safe value such as 0. Indexes stop at len(items) - 1, so items[len(items)] is always out of range, even for a one-item list. Use items[-1] or items[len(items) - 1].

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 536–590) and word search (Modules 536–622).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which list is an edge case you should test for a function that finds the biggest number?
[] (an empty list)[4, 9, 2] (a few different numbers)
Matches
biggest = nums[0] for i in range(1, len(nums) - 1): if nums[i] > biggest: biggest = nums[i]
↓
The loop stops one item early, so it never checks the last item. It fails when the maximum is at the end, for example [1, 5, 9].
Applying
Meanings
Term
edge case
Meaning
An unusual but allowed input, such as an empty list, a one-item list, repeated values, or the maximum at the start or end, that you test because it can break code.
Practice
def biggest(nums):
Testing
Gaps
if (nums) == 0:
Check Answer
Spots
2 if len(nums) == 0:
3 return nums[0]

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

The 'Blue-Ringed Octopus' badge — Reef Legend, 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. Which list is an edge case you should test for a function that finds the biggest number?
A. [] (an empty list) ✓
B. [4, 9, 2] (a few different numbers)
An empty list is allowed input but unusual. Code like max_val = nums[0] crashes on it, so test it.
2. A program finds the largest score in a list. Which test checks the 'maximum at the start' edge case?
A. [40, 55, 90, 12]
B. [90, 40, 55, 12] ✓
The biggest value (90) is in the first position. A loop that skips index 0 would miss it.
3. A program finds the largest score in a list. Which test checks the 'maximum at the end' edge case?
A. [12, 40, 55, 90] ✓
B. [12, 90, 55, 40]
The biggest value is in the last position. A loop that stops one item early, such as range(len(nums) - 1), would miss it.
4. Which test data is a single-item edge case?
A. [7, 7, 7]
B. [7] ✓
A one-item list has no other items to compare with. [7, 7, 7] is a duplicate-values edge case instead.
5. True or false: an edge case must be invalid input that the program should reject.
A. False: edge cases are unusual but allowed inputs ✓
B. True: edge cases are always invalid inputs
Edge cases are valid inputs at the unusual ends of what is allowed, such as an empty list or one item. Invalid input is abnormal data.

Match the Pairs

max([]) → Edge case: an empty list. Python raises a ValueError because there is no largest item to return.
scores = [7, 7, 7, 7] → Edge case: duplicate values. Every item is the same, so tests should check that code handles ties.
A one-item list is an edge case because the first item is also the last item and the maximum. → nums = [42] print(nums[0] == nums[-1])
The maximum at the end of a list is an edge case that tests whether a loop checks every item. → test = [3, 8, 2, 9] print(max(test) == test[-1])
biggest = nums[0] for i in range(1, len(nums) - 1): if nums[i] > biggest: biggest = nums[i] → The loop stops one item early, so it never checks the last item. It fails when the maximum is at the end, for example [1, 5, 9].
def first_item(items): return items[0] print(first_item([])) → The code never handles an empty list, so items[0] causes an IndexError. It needs a check such as if len(items) == 0 first.

Key Term

edge case
An unusual but allowed input, such as an empty list, a one-item list, repeated values, or the maximum at the start or end, that you test because it can break code.

Fill in the Blanks

1. def biggest(nums): if len(nums) == 0: return None
Hint: Type the built-in function that counts how many items a list holds, to catch an empty list.
2. scores = [42] print(scores[0]) print(scores[-1])
Hint: Type the index of the first item. In a one-item list it is also the last item.
3. nums = [5, 9, 9, 2] print(nums.count(9))
Hint: Type the list method that tells you how many times a value appears, which helps with duplicates.
4. nums = [2, 4, 9] print(nums[-1] == max(nums))
Hint: Type the negative index that picks the last item, to test if the maximum is at the end.
5. word = "" if not word: print("Empty string")
Hint: Type the keyword that flips a condition, so the check is true when the string has no characters.

Spot the Error

Question 1
A def average(nums):
B if len(nums) == 0:
C return nums[0]
D return sum(nums) / len(nums)
Line C — For an empty list there is no item at index 0, so nums[0] raises an IndexError. The empty-list case should return a safe value such as 0.
Question 2
A def last_item(items):
B return items[len(items)]
C print(last_item([7]))
D print(last_item([3, 3, 3]))
Line B — Indexes stop at len(items) - 1, so items[len(items)] is always out of range, even for a one-item list. Use items[-1] or items[len(items) - 1].
Question 3
A nums = [9, 4, 2]
B pos = 1
C for i in range(len(nums)):
D if nums[i] > nums[pos]:
E pos = i
Line B — Starting pos at 1 skips the first item, so a maximum at the start of the list (like 9) is never chosen. pos should start at 0.
Question 4
A def count_a(text)
B n = 0
C for ch in text:
D if ch == "a":
E n += 1
F return n
Line A — The def line is missing its colon, so the function will not run at all and edge cases like an empty string cannot be tested. It should be def count_a(text):
Question 5
A word = "level"
B rev = word[::-1]
C if word = rev:
D print("Palindrome")
Line C — A single = is assignment, not comparison, so this is a syntax error. Use == to compare the word with its reverse, which also handles one-letter strings.

Student with a class code? Join your class →