Records with 2-D Lists
Treat each row as one record made from several fields.
A record groups related fields about one item. A table is a collection of records. In Python, a simple way to model this using already-known structures is a 2-D list: each inner list is one record.
Predict
Read the code first. Do not run it yet.
students = [["Ava", 14, "Red"],
["Ben", 15, "Blue"]]
print(students[1][0])
print(students[0][2])- Which row is the second record?
- Which field is printed first?
- What does
students[0][2]mean?
Investigate
Use this code to explain exactly how the program works.
cars = [["AB06 HES", "Peugeot", 2010],
["GF56 RTE", "Toyota", 2006]]
for record in cars:
print(record[0], record[1])- How many records are stored?
- Which fields are output?
- What does the loop variable
recordhold on each repetition?
Fields and records
| Field position | Meaning | Example value |
|---|---|---|
| 0 | Registration | "AB06 HES" |
| 1 | Make | "Peugeot" |
| 2 | Year | 2010 |
| 3 | Mileage | 33156 |
cars = [
["AB06 HES", "Peugeot", 2010, 33156],
["GF56 RTE", "Toyota", 2006, 26875]
]
print(cars[0][1])
print(cars[1][3])Peugeot 26875
Self-study guide
A record groups several related fields that describe one item, person or event. OCR expects students to understand records and the use of 2-D arrays to emulate database-style tables containing fields and records. In Python, a simple classroom representation is a list of fixed-length rows.
For example, one row might store [product_code, product_name, price, quantity]. The important rule is that each field position has the same meaning in every record.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
record / fields | row / positions in a Python 2-D list | One row represents one record; each position is a field. |
2-D array table | list of lists | Practical Python representation used here. |
record[3] concept | record[3] | Access one field by its fixed position. |
Worked example
This example outputs low-stock products from a table of product records.
products = [
["T4578", "Baked beans", 0.60, 288],
["T5622", "Tomato soup", 0.63, 172],
["D1144", "Butter", 1.56, 17],
["BB443", "Tea bags", 1.75, 580],
["D5229", "Greek yoghurt", 0.85, 28]
]
for record in products:
if record[3] < 30:
print(record[0], record[1])D1144 Butter D5229 Greek yoghurt
- Each row stores code, name, price and quantity in the same order.
- The outer loop selects one complete record at a time.
record[3]is always the quantity field.- When quantity is below 30, fields 0 and 1 are output.
Another example
Fields from one record can be used together in a calculation.
stock = [
["Controller", 35.0, 3],
["Cable", 8.0, 10]
]
for record in stock:
value = record[1] * record[2]
print(record[0], value)Controller 105.0 Cable 80.0
- Field 1 is price and field 2 is quantity.
- The row structure lets the program calculate a value for each stored item.
Common mistakes
- Changing the meaning of a field position between records.
- Confusing a record (one row) with a field (one value inside the row).
- Changing the dimensions of an OCR-style record table when the task expects fixed structure.
Exam-ready checklist
- Identify records and fields in a table structure.
- Access fields using fixed positions.
- Loop through records and select matching rows.
- Use 2-D arrays to emulate a table of fields and records.
What you have learned
- A record groups several related fields.
- A table contains multiple records.
- A 2-D list can model records without introducing a new Python data structure.
- Use row and field indices to access individual values.