Lists – Slicing, online

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

Lists – Slicing in one minute

What students need to know

Slice from index 1 up to (not including) index 4. Step -1 traverses the list backwards, producing a reversed copy.

Start at index 1, stop before 4: 20(1) 30(2) 40(3). [:2] takes indices 0 and 1: 10, 20.

Mistakes students make

  • Use colon : for slicing, not comma.
  • Use square brackets for slicing, not parentheses.
  • Step cannot be 0: causes ValueError.
  • copy = nums doesn't copy: both point to the same list. Use nums[:] to copy.

Discover other journeys with this module

What students will learn in this module

Every question in Module 35

Quick questions

  1. [10,20,30,40,50][1:4] returns?A[20, 30, 40]B[10, 20, 30, 40]
  2. [10,20,30,40][:2] returns?A[10, 20]B[10, 20, 30]
  3. [10,20,30,40][2:] returns?A[30, 40]B[10, 20]
  4. Does list slicing modify the original?AYesBNo creates a new list
  5. [1,2,3,4,5][::-1] returns?A[1, 2, 3, 4, 5]B[5, 4, 3, 2, 1]

Key terms

Students type the meaning of:

  • list[start:stop]
  • list[:stop]
  • list[start:]
  • list[::step]

Match the pairs

nums = [1,2,3,4,5]
nums[1,3]
Match it to what it does
nums = [1,2,3]
nums[::0]
Match it to what it does
nums = [1,2,3,4,5]
nums[1:4]
Match it to what it does
nums[::-1]Match it to what it does
n = [10,20,30,40,50]
print(n[:3])
Match it to what it does
n = [1,2,3,4,5]
print(n[::2])
Match it to what it does

Fill in the blanks

  1. nums = [10, 20, 30, 40]
    print(nums[_:3])
  2. nums = [10, 20, 30, 40]
    print(nums[2:_])
  3. nums = [1, 2, 3, 4, 5]
    print(nums[::__])
  4. nums = [1, 2, 3]
    copy = nums[_]
  5. nums = [10, 20, 30, 40, 50]
    print(nums[:_])

Spot the error

Question 1

Anums = [10, 20, 30]Bpart = nums[0,2]Cprint('Slice:', part)Dprint('Done')

Question 2

Anums = [10, 20, 30, 40]Bpart = nums(1:3)Cprint('Slice:', part)Dprint('Done')

Question 3

Anums = [1, 2, 3, 4, 5]Bpart = nums[1:4:0]Cprint('Slice:', part)Dprint('Done')

Question 4

Anums = [1, 2, 3]Bcopy = numsCcopy[0] = 99D# expects nums unchanged

Question 5

Anums = [10, 20, 30, 40, 50][-1:-4]Blabel = 'Slice'Cprint('Slice:', nums)Dprint('Done')