AQA 7517, section 4.2.1.2
An array is a collection of elements that are all the same data type, stored together under one name. Each element is reached by its index, which is its position in the array.
In Python, a list is the usual equivalent. A true array has a fixed size, while a Python list can grow, but you use both in the same way for this topic.
One-dimensional arrays
A one-dimensional (1-D) array is a single row of values.
marks = [72, 58, 91, 66]
print(marks[0]) # 72 the first element (index 0)
print(marks[3]) # 66 the last element
marks[1] = 60 # change an element
print(len(marks)) # 4 the number of elements
Indexes usually start at 0, so an array of 4 elements has indexes 0, 1, 2 and 3. Using an index outside this range is an error.
A 1-D array is a good way to represent a vector (see 4.2.8.1).
Working through an array
A loop goes through every element:
total = 0
for i in range(len(marks)):
total = total + marks[i]
print(total / len(marks)) # the average
Two-dimensional arrays
A two-dimensional (2-D) array is a grid of rows and columns, like a table. Each element needs two indexes: the row, then the column. A 2-D array is a good way to represent a matrix.
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
print(grid[0][2]) # 3 row 0, column 2
print(grid[2][1]) # 8 row 2, column 1
grid[1][1] = 0
| Col 0 | Col 1 | Col 2 | |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
To visit every element you need a nested loop: an outer loop for the rows and an inner loop for the columns.
for row in range(3):
for col in range(3):
print(grid[row][col], end=" ")
print()
More than two dimensions
An n-dimensional array is a set of elements of the same type, each reached by a tuple of integers. A tuple is an ordered list of values. A 3-D array might store a temperature for each day of each week of each year, reached as temps[year][week][day].
Worked example
Task: a teacher stores three tests for each of four students in a 2-D array, with a row per student. Find the average of the second test (column 1).
scores = [
[60, 72, 55],
[80, 65, 90],
[45, 58, 62],
[70, 77, 81],
]
total = 0
for student in range(4):
total = total + scores[student][1]
print(total / 4) # 68.0
The column index stays at 1, while the loop moves the row index through each student. The second-test scores are 72, 65, 58 and 77, which add to 272, and .
Watch out: an array of 5 elements has indexes 0 to 4. Using marks[5] causes an IndexError. This "off by one" mistake is very common, so check your loop limits.
Exam tip: in a 2-D array, give the row first, then the column. If a question uses indexes starting at 1, follow the question and not your habit.
Check yourself
What does every element in an array have in common?
They are all the same data type.
Given a = [5, 8, 2, 9], what are a[0] and a[3]?
a[0] is 5 and a[3] is 9.
How would you visit every element in a 2-D array?
With a nested loop: the outer loop steps through the rows and the inner loop steps through the columns in each row.
In the grid above, what is grid[1][2]?
6: row 1, column 2.