Sets – Basics

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

Sets – Basics

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a nested list?
A A list inside another list
B A sorted list
2. grid = [[1,2],[3,4]]\ngrid[0] returns?
A [1, 2]
B 1
3. grid = [[1,2],[3,4]]\ngrid[0][1] returns?
A 1
B 2
4. grid = [[1,2],[3,4]]\ngrid[1][0] returns?
A 3
B 4
5. How do you loop through all items in a nested list?
A One for loop
B Two nested for loops

Match the Pairs

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

Left columnRight column
1g = [[1,2],[3,4]] print(g[2][0]) AError: Use g[0][1] not g[0,1]
2g = [[1,2],[3,4]] print(g[0,1]) BOutput: 2
3g = [[1,2],[3,4]] g[0][1] CValid: Returns 2
4for row in g: for item in row: print(item) DError: Row index 2 out of range
5g = [[1,2],[3,4]] print(g[1][0]) EOutput: 3
6g = [[1,2],[3,4]] print(len(g)) FOk: Nested loops to traverse

Definitions

Nested list

Fill in the Blanks

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

1.
grid = [[1, 2], [3, 4]] print(grid[_][1])
2.
grid = [[1, 2], [3, 4]] print(grid[1][_])
3.
for ___ in grid: for item in row: print(item)
4.
grid = [[1, 2], [3, 4]] print(___( grid))
5.
grid = [[1, 2], [3, 4]] grid[0][0] = __ print(grid)

Spot the Error

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

Question 1
A grid = [[1, 2], [3, 4]]
B print(grid[0, 1])
Question 2
A grid = [[1, 2], [3, 4]]
B print(grid[2][0])
Question 3
A grid = [[1, 2], [3, 4]]
B for item in grid:
C print(item) # expects individual numbers
Question 4
A grid = [1, 2], [3, 4]
B first_row = grid[0]
C print('First row:', first_row)
D print('Item:', grid[0][1])
Question 5
A grid = [[1, 2], [3, 4]]
B print(grid[0][1][0])

Answer Key

Sets – Basics
Section A: Quick Questions
Q1
A — A list inside another list
Q2
A — [1, 2]
Q3
B — 2
Q4
A — 3
Q5
B — Two nested for loops
Section B: Match the Pairs
Q1
g = [[1,2],[3,4]] print(g[2][0]) → Error: Row index 2 out of range
Q2
g = [[1,2],[3,4]] print(g[0,1]) → Error: Use g[0][1] not g[0,1]
Q3
g = [[1,2],[3,4]] g[0][1] → Valid: Returns 2
Q4
for row in g: for item in row: print(item) → Ok: Nested loops to traverse
Q5
g = [[1,2],[3,4]] print(g[1][0]) → Output: 3
Q6
g = [[1,2],[3,4]] print(len(g)) → Output: 2
Section C: Definitions
Q1
Nested list — A list that contains other lists as elements.~~grid[row][col]
Section D: Fill in the Blanks
Q1
grid = [[1, 2], [3, 4]] print(grid[0][1])
Q2
grid = [[1, 2], [3, 4]] print(grid[1][1])
Q3
for row in grid: for item in row: print(item)
Q4
grid = [[1, 2], [3, 4]] print(len( grid))
Q5
grid = [[1, 2], [3, 4]] grid[0][0] = 99 print(grid)
Section E: Spot the Error
Q1
B — Use two separate brackets grid[0][1], not a comma grid[0, 1]
Q2
B — grid has indices 0 and 1. Index 2 is out of range.
Q3
B — One loop gives inner lists [1,2] and [3,4]. Need nested loops for individual items.
Q4
A — Missing outer brackets: this creates a tuple of lists, not a list of lists
Q5
B — grid[0][1] is 2 (an integer). Can't index into a number.