Community resourceWorksheet
1CP2-CT-10.2 Traversing displaying and searching two-dimensional data
Part 2 of 5 · 1CP2-CT-10 · Integrated programming and data structures
An outer loop selects each record. Direct field indexing or an inner loop accesses fields inside that record. For a search, compare only the named key field and preserve the complete matched row when the requirement asks for the record.
Students will:
- traverse records and their fields systematically
- predict nested traversal before execution
- search using the named key field
- construct a search that preserves the complete matched row
Inside: 6 explanation cells, 1 fill-in-the-blanks cell, 2 multiple-choice questions, 4 written answers, 1 trace table and 1 Python task. 19 marks, about 45 minutes.
Series: 1CP2-CT-10 · Integrated programming and data structures, part 2 of 5.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Traversing, displaying and searching two-dimensional data
An outer loop selects each record. Direct field indexing or an inner loop accesses fields inside that record. For a search, compare only the named key field and preserve the complete matched row when the requirement asks for the record.
1. Follow the traversal path
To display each record in columns, use the same field positions for every row. To search by name, compare row[0] with the target. Start a found flag as False; change it only when a match occurs.
found as gap 3.- False
- inside
- outer
2. Predict before execution
records = [["Asha", 12], ["Ben", 18], ["Cara", 14]]
for row in records:
if row[1] >= 14:
print(row[0])
Which names are printed, in order?
- AAsha only
- BBen then Cara
- CCara then Ben
- DAll three names
Explain why Cara is printed even though her score is not greater than 14.
Refer to the exact operator.
Students type their answer here.
Complete the record-search trace.
Record row, comparisons, found and matched_name.
Use one row for each pass through the loop. Fill in a box only when that value changes on that row, and leave the rest blank.
records = [["Asha", 12], ["Ben", 18], ["Cara", 14]]target = "Ben"found = Falsecomparisons = 0matched_name = ""for row in records:comparisons = comparisons + 1if row[0] == target and found == False:found = Truematched_name = row[0]print(matched_name)
| Row | records | target | found | comparisons | matched_name | row | row[0] == target and found == False | Output |
|---|---|---|---|---|---|---|---|---|
| 1 | ||||||||
| 2 | ||||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | ||||||||
| 6 | ||||||||
| 7 |
Visit every field with nested traversal
When every field must be processed, the outer loop selects each row. An inner loop then selects each field in that row: for row in records: followed by an indented for field in row:. The inner loop completes all fields before the outer loop moves to the next record.
For a 3-row structure with 4 fields in every row, how many times does the inner-loop body execute, and why?
Link rows and fields.
Students type their answer here.
3. Construct a record search
Define find_record(records, target) to return the complete first row whose field 0 equals target, or an empty list if absent. Use linear search; do not sort the records.
records = [["Asha", "Red", 68], ["Ben", "Blue", 73], ["Cara", "Red", 81]]
# Define find_record and use it to find Ben.
Give one found and one not-found test for find_record, with exact expected returned values.
Use the supplied records.
Students type their answer here.
Why should this search compare only row[0]?
- AThe requirement defines field 0 as the name key
- BOther fields cannot contain data
- CA loop cannot access field 1
- DSearching every field sorts the table
The target is in the second of 100 rows. State one refinement that could avoid unnecessary later comparisons and explain its effect.
Stay within PLS-compatible Boolean control.
Students type their answer here.
Route forward
You can traverse, display and search records. Next you will load typed records from a supplied CSV and keep loading, searching and display in focused subprograms.