What is a List?
A list is an ordered collection of items. In HKDSE ICT, lists are the most tested data structure after integers and strings.
scores = [85, 72, 91, 68, 79] names = ["Chan", "Lee", "Wong"] mixed = [1, "hello", 3.14, True] # Lists can hold anything
Indexing
Each item has a position (index) starting from 0:
scores = [85, 72, 91, 68, 79] # 0 1 2 3 4 (indexes) # -5 -4 -3 -2 -1 (negative indexes) print(scores[0]) # 85 (first) print(scores[-1]) # 79 (last) print(scores[2]) # 91
Slicing
scores = [85, 72, 91, 68, 79] print(scores[1:4]) # [72, 91, 68] print(scores[:3]) # [85, 72, 91] print(scores[2:]) # [91, 68, 79] print(scores[::-1]) # [79, 68, 91, 72, 85] (reversed)
Modifying Lists
scores = [85, 72, 91] # Change an item scores[0] = 90 # scores is now [90, 72, 91] # Add to the end scores.append(68) # [90, 72, 91, 68] # Insert at position scores.insert(1, 100) # [90, 100, 72, 91, 68] # Remove by value scores.remove(72) # [90, 100, 91, 68] # Remove by index removed = scores.pop(0) # removed = 90, scores = [100, 91, 68] # Clear everything scores.clear() # []
Useful Functions
nums = [3, 1, 4, 1, 5, 9, 2, 6] len(nums) # 8 (length) sum(nums) # 31 max(nums) # 9 min(nums) # 1 sorted(nums) # [1, 1, 2, 3, 4, 5, 6, 9] (new list) nums.sort() # Sort in place nums.reverse() # Reverse in place nums.count(1) # 2 (how many 1s) nums.index(5) # position of first 5
Iterating Over Lists
scores = [85, 72, 91, 68, 79]
# Simple iteration
for score in scores:
print(score)
# With index
for i, score in enumerate(scores):
print(f"Student {i+1}: {score}")
# Calculate total
total = 0
for score in scores:
total += score
List Comprehension: The Pythonic Way
A concise way to create new lists from existing ones:
# Long way
squares = []
for n in range(10):
squares.append(n * n)
# Pythonic way (same result)
squares = [n * n for n in range(10)]
# With condition
evens = [n for n in range(20) if n % 2 == 0]
# Transform strings
names = ["chan", "lee", "wong"]
upper = [name.upper() for name in names]
# ["CHAN", "LEE", "WONG"]
2D Lists (Lists of Lists)
Essential for matrices and grids:
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(grid[1][2]) # 6 (row 1, column 2)
# Iterate all cells
for row in grid:
for val in row:
print(val, end=" ")
print()
Common Pitfalls
Copying Lists
a = [1, 2, 3] b = a # Both variables point to SAME list b.append(4) print(a) # [1, 2, 3, 4] — a changed too! # To copy: b = a.copy() # or a[:] or list(a)
Modifying While Iterating
lst = [1, 2, 3, 4, 5]
for x in lst:
if x % 2 == 0:
lst.remove(x) # ✗ Dangerous, skips items
# Better: iterate over a copy
for x in lst[:]:
if x % 2 == 0:
lst.remove(x)
Real-World Example
# Class score analysis
scores = [85, 72, 91, 68, 79, 45, 82, 90]
print(f"Students: {len(scores)}")
print(f"Average: {sum(scores)/len(scores):.1f}")
print(f"Highest: {max(scores)}")
print(f"Lowest: {min(scores)}")
print(f"Passes: {len([s for s in scores if s >= 50])}")
print(f"Top 3: {sorted(scores, reverse=True)[:3]}")
Master Lists in 30 Minutes
Run every example above in PyForm. The best way to learn lists is to manipulate them interactively.
Try PyForm →