Data Structures: Arrays, Records and File Design
Why Data Structures Matter
When a program needs to store more than a few values, single variables become unmanageable. Imagine tracking the scores of 30 students with 30 separate variables — the code would be enormous and impossible to loop over. Data structures solve this by grouping related data together under one name. The three structures you need to understand are one-dimensional arrays, two-dimensional arrays and records. Each suits a different kind of problem.
One-Dimensional Arrays
A one-dimensional array is a list of items of the same data type, stored in a row. Every item is accessed using an index. In Python, indexing starts at 0, so the first element of an array called scores is scores[0].
# Declaring a 1D array of five exam scores
scores = [72, 85, 61, 90, 78]
# Accessing the third element (index 2)
print(scores[2]) # outputs 61
# Changing an element
scores[0] = 75
# Looping over the whole array
for i in range(len(scores)):
print(scores[i])
Arrays store items of the same type — all integers, all strings, and so on. This is what makes them efficient: the computer knows the size of each element, so it can jump directly to any index in one step. You cannot mix data types in a single array.
A 1D array stores a fixed set of same-type values in a single row. Access is by index number. In Python, the first index is 0, so an array of length 6 has valid indices 0 to 5.
Two-Dimensional Arrays
A two-dimensional array is a grid — think of a table or a spreadsheet. Each element needs two indices: a row index and a column index. A seating plan for a hall with 5 rows and 8 columns could be stored as a 2D array with 40 elements.
# 2D array: 3 students, 4 test scores each
results = [
[72, 85, 61, 90], # student 0
[55, 70, 80, 65], # student 1
[88, 92, 76, 84] # student 2
]
# Access student 1's third test score (row 1, column 2)
print(results[1][2]) # outputs 80
# Loop over all scores
for row in range(len(results)):
for col in range(len(results[row])):
print(results[row][col])
The outer index selects the row; the inner index selects the column. A 2D array is ideal when data is naturally tabular — for example, a timetable or a grid-based game map. Like 1D arrays, all elements must be the same data type.
A 2D array uses two indices: array[row][column]. All elements must share the same data type. A 2D array of r rows and c columns holds r × c elements in total.
Arrays require all elements to be the same type. But a student record typically contains a name (string), an age (integer), a registration number (string) and a grade (character) — all different types. A record stores different data types together under one structured name. Each piece of information within a record is called a field.
In Python, a record is commonly modelled as a dictionary or a class. For GCSE purposes, describe a record by listing its fields, their data types and their purposes:
# A student record with four fields
student = {
"name": "Aisha Patel", # string
"age": 16, # integer
"reg_number": "24A017", # string
"grade": "B" # character
}
print(student["name"]) # outputs Aisha Patel
Each field in a record has a name and a data type. Fields can be strings, integers, booleans, dates — anything. This flexibility is the key difference between a record and an array.
A file is a collection of related records stored on a secondary storage device (hard drive, SSD). A school database might store one record per student, with all records forming a file. When designing a file, you decide which fields each record needs and what data type and size each field should have.
A simple file design table for a student file might look like this:
| Field name | Data type | Example value | Purpose |
|---|---|---|---|
| StudentID | String | 24A017 | Unique identifier for each student |
| Surname | String | Patel | Student's family name |
| Forename | String | Aisha | Student's given name |
| YearGroup | Integer | 11 | Current school year |
| FreeSchoolMeals | Boolean | True | Entitlement flag for funding |
Good file design avoids storing the same data twice (redundancy) and keeps each field focused on one piece of information. For example, storing “full name” as a single field makes it harder to sort by surname — separate Surname and Forename fields are better design.
The rule is straightforward: if all your data is the same type and you need to loop over it numerically, use an array. If your data has mixed types and represents one real-world entity, use a record. If you need to store many records persistently on disk, you need a file.
Examples to practise:
- Monthly rainfall figures for 12 months — all the same type (real numbers), naturally sequential. Use a 1D array.
- Rainfall figures for 12 months across 5 cities — same type, two dimensions (city × month). Use a 2D array.
- Details about one patient (name, date of birth, blood group, NHS number) — mixed types, one entity. Use a record.
- Details about thousands of patients stored permanently — many records. Use a file of patient records.