← VCE Software Development U3O1

Accessing a record's fields by name and by field index

Gets a field from a record by its field name. A list or tuple record uses the field index (its position) instead, such as student[0]. Indexes start at 0.

Understanding Accessing a record's fields by name and by field index in VCE Software Development U3O1

What it does: Round brackets create a tuple. Its fields are read by index, but student[1] = 18 would cause an error. Unpacking assigns the fields in order to the variables, so name gets student[0], age gets student[1] and year gets student[2]. A dictionary record is accessed by its field name written as a string in quotes. Without quotes, Python looks for a variable called name. Field indexes start at 0, so the first field is at index 0. Python counts positions from 0. The field at index 1 is the second field. Unpacking matches each field to a variable by position. The second line gives all three variables the whole tuple.

Common mistakes: A dictionary record's field name must be written as a string in quotes. Without quotes, Python looks for a variable called name and raises a NameError. It should be print(s['name']). The tuple has 3 fields, so the field indexes are 0, 1 and 2. Index 3 is out of range and causes an IndexError. The house is at index 2: print(student[2]).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A record is stored as a dictionary: student = {'name': 'Ava', 'year': 11}. Which line gets the student's name?
student[name]student['name']
Matches
student = ['Ava', 17, 'Year 12'] print(student[3])
↓
Index 3 is out of range. The record has 3 fields, so the valid indexes are 0, 1 and 2. Use student[2] for the last field.
Applying
Meanings
Term
student['name']
Meaning
Gets a field from a record by its field name. A list or tuple record uses the field index (its position) instead, such as student[0]. Indexes start at 0.
Practice
student = {'name': 'Ava', 'year': 9}
Testing
Gaps
print(student[])
Check Answer
Spots
1s = {'name': 'Ava', 'yr': 9}
2print(s[name])

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Lion' badge — Inheritance King, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A record is stored as a dictionary: student = {'name': 'Ava', 'year': 11}. Which line gets the student's name?
A. student[name]
B. student['name'] ✓
A dictionary record is accessed by its field name written as a string in quotes. Without quotes, Python looks for a variable called name.
2. A record is stored as a list: student = ['Ava', 11, 'Red']. Which line gets the first field, 'Ava'?
A. student[0] ✓
B. student[1]
Field indexes start at 0, so the first field is at index 0.
3. Which statement about field indexes is true?
A. The first field in a list or tuple record has index 1
B. The first field in a list or tuple record has index 0 ✓
Python counts positions from 0. The field at index 1 is the second field.
4. Given student = ('Ava', 11, 'Red'), which line gives an error?
A. student[1] = 12 ✓
B. print(student[1])
A tuple is an unchangeable record. You can read its fields by index, but you cannot assign a new value to one.
5. Given student = {'name': 'Ava', 'year': 11}, which line is the better way to get the year?
A. student['year'] ✓
B. student[1]
A dictionary record uses field names as keys. A field name tells the reader what the value means, but a number does not.

Match the Pairs

student['name'] → Gets the value of the field called name from a dictionary record
student[0] → Gets the first field of a list or tuple record by its field index
A tuple is an unchangeable record, so its fields can be read but not reassigned → student = ('Ava', 17, 'Year 12')
Unpacking copies each field of a record into its own variable in one line → name, age, year = student
student = ['Ava', 17, 'Year 12'] print(student[3]) → Index 3 is out of range. The record has 3 fields, so the valid indexes are 0, 1 and 2. Use student[2] for the last field.
student = {'name': 'Ava', 'age': 17} print(student[name]) → The field name is missing quotes, so Python looks for a variable called name. It should be student['name'].

Key Term

student['name']
Gets a field from a record by its field name. A list or tuple record uses the field index (its position) instead, such as student[0]. Indexes start at 0.

Fill in the Blanks

1. student = ['Ava', 7, 'Red'] print(student[0])
Hint: Type the field index of the first field. Indexes start counting from zero.
2. student = ('Ava', 7, 'Red') print(student[2])
Hint: Type the field index of the last of the three fields (the house).
3. student = {'name': 'Ava', 'year': 7} print(student['year'])
Hint: Type the field name (without the quotes) that holds the year level.
4. student = ('Ava', 7, 'Red') name, year, house = student print(name)
Hint: Type the variable that holds the record being unpacked into three variables.
5. student = {'name': 'Ava', 'year': 7} print(student['name'])
Hint: Type the name of the record variable that the field name is used on.

Spot the Error

Question 1
A s = {'name': 'Ava', 'yr': 9}
B print(s[name])
C print(s['yr'])
Line B — A dictionary record's field name must be written as a string in quotes. Without quotes, Python looks for a variable called name and raises a NameError. It should be print(s['name']).
Question 2
A student = ('Ava', 9, 'Red')
B print(student[0])
C print(student[3])
Line C — The tuple has 3 fields, so the field indexes are 0, 1 and 2. Index 3 is out of range and causes an IndexError. The house is at index 2: print(student[2]).
Question 3
A student = ['Ava', 9, 'Red']
B print(student['year'])
C print(student[0])
Line B — A list record has no field names, so you must use the field index (its position). The year is the second field, so it is student[1]. Using a string name on a list gives a TypeError.
Question 4
A student = ('Ava', 9)
B name = student[0]
C student[1] = 10
D print(name, year)
Line C — A tuple is an unchangeable record, so you cannot assign a new value to one of its fields. This causes a TypeError. To read the field, use year = student[1].
Question 5
A student = ('Ava', 9, 'Red')
B print(student[0])
C name, year = student
D print(name, house)
Line C — When unpacking a record, the number of variables must match the number of fields. The tuple has 3 fields but only 2 variables are given, causing a ValueError. It should be name, year, house = student.

Student with a class code? Join your class →