Loading...
Choose a representation that makes the meaning of each position clear. A three-dimensional array needs three independent coordinates, while a record groups named fields that need not share a type.
What you'll be able to do
A one-dimensional array can store scores for students; a two-dimensional array can use student and test coordinates. Add a third coordinate for term to obtain scores[term,student,test] under an agreed order. A dimension’s length is a count, not its final zero-based index. Lengths 2,3,4 therefore allow term 0–1, student 0–2 and test 0–3.
The array contains 2×3×4=24 positions. Traversing all values needs a loop over each dimension, with bounds derived from their lengths. A correct loop can still report the wrong interpretation if term and student are swapped. Document coordinate order and use a small distinctive test value at a known position to check it.
A record groups related named attributes, such as StudentID, Name and Active. The fields can have different types because their meanings differ. A list stores an ordered sequence and often provides dynamic operations such as insertion; an array model may instead have a declared fixed size and homogeneous element type. Language implementations vary, so follow the model stated.
A tuple is an ordered collection whose positions have meaning; Python tuples are immutable sequences. Immutability means the tuple’s own element bindings cannot be reassigned, not that every object referred to by a tuple is recursively immutable. A tuple containing a list can still refer to a list that changes. Choose named records when anonymous position numbers would obscure the data model.
Python nested lists can model a three-dimensional collection, but they are not automatically a contiguous fixed-size array. In the example, each comprehension creates a separate inner list for its row. Assigning grid[1][2][3]=9 should change only that intended position. Repeating the same mutable row reference can make several apparent rows change together.
Test construction as well as access: check lengths at each level, the total number of cells and that a write does not affect another row or plane. Validate coordinates before using untrusted input as indices. Python negative indices have their own language meaning; they should not silently be accepted as valid coordinates in a zero-to-length-minus-one application contract.
grid = [[[0 for test in range(4)]
for student in range(3)]
for term in range(2)]
grid[1][2][3] = 9
print(grid[1][2][3], grid[0][2][3], grid[1][0][3])
print(sum(len(row) for plane in grid for row in plane))
entry = (17, "Amina", True)
print(entry[1])Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.