Lists – Indexing, online

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

Lists – Indexing in one minute

What students need to know

Unlike strings, list items can be changed by assigning to their index. -1 always gives the last element of a list.

Index 0 is always the first item. Indices go from 0 to len-1 — [3] doesn't exist in a 3-item list.

Mistakes students make

  • List has indices 0,1,2: index 3 is out of range.
  • Use square brackets [] for indexing, not parentheses ().
  • Index must be an integer, not a string.
  • Use square brackets for assignment: nums[1] = 99.

Discover other journeys with this module

What students will learn in this module

Every question in Module 34

Quick questions

  1. colors = ['red','blue','green']\ncolors[0] returns?A'blue'B'red'
  2. colors = ['red','blue','green']\ncolors[-1] returns?A'green'B'red'
  3. Can you change a list item by index?ANo lists are immutableBYes lists are mutable
  4. nums = [10,20,30]\nnums[3] does what?AIndexErrorBReturns 30
  5. 'cat' in ['cat','dog'] returns?ATrueBFalse

Key terms

Students type the meaning of:

  • list[0]
  • list[-1]
  • list[i] = value
  • IndexError

Match the pairs

nums = [10, 20, 30]
print(nums[3])
Match it to what it does
nums = [1, 2, 3]
print(nums(0))
Match it to what it does
nums = [5, 10, 15]
nums[1] = 99
Match it to what it does
nums[-1]Match it to what it does
c = ['r','g','b']
print(c[0])
Match it to what it does
c = ['r','g','b']
print(c[-1])
Match it to what it does

Fill in the blanks

  1. colors = ['red', 'blue']
    print(colors[_])
  2. nums = [10, 20, 30]
    nums[_] = 99
  3. pets = ['cat', 'dog']
    print('cat' __ pets)
  4. ___ fruit in fruits:
        print(fruit)
  5. colors = ['red', 'blue', 'green']
    print(colors[__])

Spot the error

Question 1

Acolors = ['red', 'blue', 'green']Bfirst = colors[0]Clast = colors[3]Dprint(first, last)

Question 2

Acolors = ['red', 'blue']Bfirst = colors(0)Cprint('First:', first)Dprint('Done')

Question 3

Acolors = ['red', 'blue']Bfirst = colors['0']Cprint('First:', first)Dprint('Done')

Question 4

Anums = [10, 20, 30]Bnums(1) = 99Cprint('Updated:', nums)Dprint('Done')

Question 5

Acolors = ['red', 'blue']Bfor color in colorsC    print(color)Dprint('Done')