Single- and Multi-dimensional Arrays
intermediate25 minLearning objectives
- Explain how arrays store collections of data
- Access and manipulate array elements using indexes
- Compare one- and two-dimensional arrays
- Evaluate the strengths and limitations of arrays
Learn
AQA 4.2.2 — Arrays
Retrieval: Sequence 3's functions took single values as parameters (a name, a score); arrays let a single variable hold a whole collection of values instead - the natural next step once one value at a time stops being enough.
An array is a fixed-size, ordered collection where every element is accessed by an index. AQA's specification (and exam pseudocode) treats arrays as their own concept, distinct from more advanced Python-specific structures — in Python, a list is used to model an array.
scores = [72, 85, 91, 68, 77]
print(scores[0]) # 72 - first element (index 0)
print(scores[-1]) # 77 - last element
print(len(scores)) # 5
Two-dimensional arrays
A 2D array is an array of arrays — useful for grids, seating plans, or timetables.
seating_plan = [
["Aisha", "Tom", "Priya"],
["Liam", "Sofia", "Omar"],
]
print(seating_plan[0][1]) # "Tom" - row 0, column 1
Strengths and limitations
Strengths: O(1) direct access by index; simple, predictable memory layout. Limitations: fixed size in many languages (Python lists resize dynamically, but AQA's theoretical array model doesn't); inserting/removing in the middle requires shifting elements — O(n).
Common mistake
Because a Python list resizes automatically, it's easy to assume "arrays" are just as flexible in every language and in AQA's theoretical model — they aren't. If an exam question specifies a fixed-size array, assume operations like inserting into the middle require shifting every element after it, which is why understanding an array's limitations (not just its Python convenience) matters.
Worked example — a seating plan manager
def find_student(seating_plan, name):
for row_index, row in enumerate(seating_plan):
for col_index, student in enumerate(row):
if student == name:
return row_index, col_index
return None
position = find_student(seating_plan, "Sofia")
print(position) # (1, 1)
Challenge
Create a 2D array representing a weekly timetable (5 days × 5 periods) and write a function that returns the lesson at a given day and period.