131.3 Records

Store and manage related data values of different types using structured data formats.

Outline

In this topic, we introduce records—a data structure that allows you to group related data under one name using named fields. Records are instrumental when storing multiple attributes about the same object, such as a user or a product.

Targets

In this topic, students learn to:

  • Understand what a record is and how it differs from lists or arrays

  • Define records using dictionaries or classes in Python

  • Store and access multiple fields of different types under a single data item

  • Use records to structure data in more meaningful ways for real-world problems

Glossary

Term
Definition

Record

A data structure that stores related fields under one entity, often with different data types

Field

An individual attribute in a record, e.g. name, age, email

Key-value pair

A way of storing data where each key (field name) maps to a specific value

Dictionary

A Python data structure often used to represent records with keys and values

Class

A Python structure that defines a new data type (used in object-oriented programming to create records)

Overview

A record groups together values that belong to one object or entity. These values can be of different data types. For example, a record representing a student might include a name (string), age (integer), and enrolled status (Boolean).

In other languages, records are sometimes called structs or objects. In Python, you can implement records using dictionaries (for quick prototyping) or classes (for more structured design).

Records using dictionaries

In Python, dictionaries are ideal for creating simple records.

student = {
    "name": "Aria",
    "age": 17,
    "enrolled": True
}

You can access fields using their names:

print(student["name"])      # Output: Aria
print(student["enrolled"])  # Output: True

You can update fields:

student["age"] = 18

And add new ones:

student["grade"] = "A"

Records using classes

For larger programs or when structure and type checking matter, we define records using classes.

class Student:
    def __init__(self, name, age, enrolled):
        self.name = name
        self.age = age
        self.enrolled = enrolled

s1 = Student("Aria", 17, True)
print(s1.name)  # Output: Aria

This approach allows you to create multiple instances and ensures consistency in how data is organised.

Lists of records

You can store multiple records in a list. This is useful for managing collections of data, such as student or employee records.

students = [
    {"name": "Aria", "age": 17},
    {"name": "Liam", "age": 16},
    {"name": "Zoe", "age": 17}
]

for student in students:
    print(student["name"], student["age"])

When to use records

Use records when:

  • You need to group different types of data under a common heading

  • Your data has named fields (like name, age, height)

  • You want to organise information logically for readability and maintainability

Key concepts

  • A record is a structured way of grouping related data fields

  • Dictionaries and classes in Python can be used to implement records

  • Records are more expressive than lists when data has named fields

  • Lists of records allow for scalable, structured storage of complex data

Last updated

Was this helpful?