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

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

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

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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']
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]
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
4. Given student = ('Ava', 11, 'Red'), which line gives an error?
A student[1] = 12
B print(student[1])
5. Given student = {'name': 'Ava', 'year': 11}, which line is the better way to get the year?
A student['year']
B student[1]

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1student['name'] AGets the value of the field called name from a dictionary record
2student[0] BIndex 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.
3A tuple is an unchangeable record, so its fields can be read but not reassigned Cname, age, year = student
4Unpacking copies each field of a record into its own variable in one line Dstudent = ('Ava', 17, 'Year 12')
5student = ['Ava', 17, 'Year 12'] print(student[3]) EThe field name is missing quotes, so Python looks for a variable called name. It should be student['name'].
6student = {'name': 'Ava', 'age': 17} print(student[name]) FGets the first field of a list or tuple record by its field index

Definitions

student['name']

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
student = ['Ava', 7, 'Red'] print(student[_])
2.
student = ('Ava', 7, 'Red') print(student[_])
3.
student = {'name': 'Ava', 'year': 7} print(student['____'])
4.
student = ('Ava', 7, 'Red') name, year, house = _______ print(name)
5.
student = {'name': 'Ava', 'year': 7} print(_______['name'])

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A s = {'name': 'Ava', 'yr': 9}
B print(s[name])
C print(s['yr'])
Question 2
A student = ('Ava', 9, 'Red')
B print(student[0])
C print(student[3])
Question 3
A student = ['Ava', 9, 'Red']
B print(student['year'])
C print(student[0])
Question 4
A student = ('Ava', 9)
B name = student[0]
C student[1] = 10
D print(name, year)
Question 5
A student = ('Ava', 9, 'Red')
B print(student[0])
C name, year = student
D print(name, house)

Answer Key

Accessing a record's fields by name and by field index
Section A: Quick Questions
Q1
B — student['name']
Q2
A — student[0]
Q3
B — The first field in a list or tuple record has index 0
Q4
A — student[1] = 12
Q5
A — student['year']
Section B: Match the Pairs
Q1
student['name'] → Gets the value of the field called name from a dictionary record
Q2
student[0] → Gets the first field of a list or tuple record by its field index
Q3
A tuple is an unchangeable record, so its fields can be read but not reassigned → student = ('Ava', 17, 'Year 12')
Q4
Unpacking copies each field of a record into its own variable in one line → name, age, year = student
Q5
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.
Q6
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'].
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
student = ['Ava', 7, 'Red'] print(student[0])
Q2
student = ('Ava', 7, 'Red') print(student[2])
Q3
student = {'name': 'Ava', 'year': 7} print(student['year'])
Q4
student = ('Ava', 7, 'Red') name, year, house = student print(name)
Q5
student = {'name': 'Ava', 'year': 7} print(student['name'])
Section E: Spot the Error
Q1
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']).
Q2
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]).
Q3
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.
Q4
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].
Q5
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.