Sets – Basics, online

Six activities your students do on their own devices. See who's finished, and their scores.

Sets – Basics in one minute

What students need to know

g[0] is [1,2], then [1] gives the second item: 2. A nested for loop visits every element in every inner list.

Any list item can itself be a list — nesting creates multi-dimensional structures. grid[0] gets the first inner list — a second index is needed to get a number.

Mistakes students make

  • Use two separate brackets grid[0][1], not a comma grid[0, 1].
  • grid has indices 0 and 1. Index 2 is out of range.
  • One loop gives inner lists [1,2] and [3,4]. Need nested loops for individual items.
  • Missing outer brackets: this creates a tuple of lists, not a list of lists.

Discover other journeys with this module

What students will learn in this module

Every question in Module 40

Quick questions

  1. What is a nested list?AA list inside another listBA sorted list
  2. grid = [[1,2],[3,4]]\ngrid[0] returns?A[1, 2]B1
  3. grid = [[1,2],[3,4]]\ngrid[0][1] returns?A1B2
  4. grid = [[1,2],[3,4]]\ngrid[1][0] returns?A3B4
  5. How do you loop through all items in a nested list?AOne for loopBTwo nested for loops

Key terms

Students type the meaning of:

  • Nested list
  • grid[row][col]
  • Inner list
  • Nested loop

Match the pairs

g = [[1,2],[3,4]]
print(g[2][0])
Match it to what it does
g = [[1,2],[3,4]]
print(g[0,1])
Match it to what it does
g = [[1,2],[3,4]]
g[0][1]
Match it to what it does
for row in g:
    for item in row:
        print(item)
Match it to what it does
g = [[1,2],[3,4]]
print(g[1][0])
Match it to what it does
g = [[1,2],[3,4]]
print(len(g))
Match it to what it does

Fill in the blanks

  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

Question 1

Agrid = [[1, 2], [3, 4]]Bprint(grid[0, 1])

Question 2

Agrid = [[1, 2], [3, 4]]Bprint(grid[2][0])

Question 3

Agrid = [[1, 2], [3, 4]]Bfor item in grid:C    print(item)  # expects individual numbers

Question 4

Agrid = [1, 2], [3, 4]Bfirst_row = grid[0]Cprint('First row:', first_row)Dprint('Item:', grid[0][1])

Question 5

Agrid = [[1, 2], [3, 4]]Bprint(grid[0][1][0])