20-records.py
python-programming / further-programming-techniques  ·  Lesson 21
Lesson 21 — Further programming techniques

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.

records-predict.py
students = [["Ava", 14, "Red"],
            ["Ben", 15, "Blue"]]
print(students[1][0])
print(students[0][2])
Predict before running
  • 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.

records-investigate.py
cars = [["AB06 HES", "Peugeot", 2010],
        ["GF56 RTE", "Toyota", 2006]]
for record in cars:
    print(record[0], record[1])
Investigate the code
  • How many records are stored?
  • Which fields are output?
  • What does the loop variable record hold on each repetition?

Fields and records

Field positionMeaningExample value
0Registration"AB06 HES"
1Make"Peugeot"
2Year2010
3Mileage33156
records.py
cars = [
    ["AB06 HES", "Peugeot", 2010, 33156],
    ["GF56 RTE", "Toyota", 2006, 26875]
]

print(cars[0][1])
print(cars[1][3])
Output
Peugeot
26875
Record terminologyOne row is a record. Each individual value in that row is a field.

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 focusUnderstand records as groups of fields and use 2-D arrays to emulate table-like collections of records. Keep the field order consistent.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
record / fieldsrow / positions in a Python 2-D listOne row represents one record; each position is a field.
2-D array tablelist of listsPractical Python representation used here.
record[3] conceptrecord[3]Access one field by its fixed position.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This example outputs low-stock products from a table of product records.

lesson-21-worked.py
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])
Output
D1144 Butter
D5229 Greek yoghurt
Work through it line by line
  • 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.

lesson-21-example-2.py
stock = [
    ["Controller", 35.0, 3],
    ["Cable", 8.0, 10]
]

for record in stock:
    value = record[1] * record[2]
    print(record[0], value)
Output
Controller 105.0
Cable 80.0
Why this example matters
  • Field 1 is price and field 2 is quantity.
  • The row structure lets the program calculate a value for each stored item.

Common mistakes

Avoid these errors
  • 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-safe warningOCR Exam Reference Language notes that arrays store a single data type, while Python lists can mix types. Mixed-type Python rows here are being used to emulate records, which OCR requires separately; do not use this as evidence that an OCR array itself can mix data types.

Exam-ready checklist

You should now be able to
  • 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.