Lists – Slicing

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

Lists – Slicing

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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?
A Yes
B No creates a new list
5. [1,2,3,4,5][::-1] returns?
A [1, 2, 3, 4, 5]
B [5, 4, 3, 2, 1]

Match the Pairs

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

Left columnRight column
1nums = [1,2,3,4,5] nums[1,3] AError: Slice step cannot be zero
2nums = [1,2,3] nums[::0] BOutput: [1, 3, 5]
3nums = [1,2,3,4,5] nums[1:4] COk: Reverses the list
4nums[::-1] DOutput: [10, 20, 30]
5n = [10,20,30,40,50] print(n[:3]) EValid: Returns [2, 3, 4]
6n = [1,2,3,4,5] print(n[::2]) FError: Use colon for slicing

Definitions

list[start:stop]

Fill in the Blanks

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

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

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

Question 1
A nums = [10, 20, 30]
B part = nums[0,2]
C print('Slice:', part)
D print('Done')
Question 2
A nums = [10, 20, 30, 40]
B part = nums(1:3)
C print('Slice:', part)
D print('Done')
Question 3
A nums = [1, 2, 3, 4, 5]
B part = nums[1:4:0]
C print('Slice:', part)
D print('Done')
Question 4
A nums = [1, 2, 3]
B copy = nums
C copy[0] = 99
D # expects nums unchanged
Question 5
A nums = [10, 20, 30, 40, 50][-1:-4]
B label = 'Slice'
C print('Slice:', nums)
D print('Done')

Answer Key

Lists – Slicing
Section A: Quick Questions
Q1
A — [20, 30, 40]
Q2
A — [10, 20]
Q3
A — [30, 40]
Q4
B — No creates a new list
Q5
B — [5, 4, 3, 2, 1]
Section B: Match the Pairs
Q1
nums = [1,2,3,4,5] nums[1,3] → Error: Use colon for slicing
Q2
nums = [1,2,3] nums[::0] → Error: Slice step cannot be zero
Q3
nums = [1,2,3,4,5] nums[1:4] → Valid: Returns [2, 3, 4]
Q4
nums[::-1] → Ok: Reverses the list
Q5
n = [10,20,30,40,50] print(n[:3]) → Output: [10, 20, 30]
Q6
n = [1,2,3,4,5] print(n[::2]) → Output: [1, 3, 5]
Section C: Definitions
Q1
list[start:stop] — Get items from start up to but not including stop.~~list[:stop]
Section D: Fill in the Blanks
Q1
nums = [10, 20, 30, 40] print(nums[0:3])
Q2
nums = [10, 20, 30, 40] print(nums[2:4])
Q3
nums = [1, 2, 3, 4, 5] print(nums[::-1])
Q4
nums = [1, 2, 3] copy = nums[:]
Q5
nums = [10, 20, 30, 40, 50] print(nums[:2])
Section E: Spot the Error
Q1
B — Use colon : for slicing, not comma
Q2
B — Use square brackets for slicing, not parentheses
Q3
B — Step cannot be 0: causes ValueError
Q4
B — copy = nums doesn't copy: both point to the same list. Use nums[:] to copy.
Q5
A — Start must come before stop: -4 comes before -1