Chapter 2: Lists

List slicing

face Josiah Wang

Python also provides a convenient way to perform list slicing to slice out a subset of your list.

>>> students = ["Abraham", "Bella", "Connor", "Da-ming", "Enya"]
>>> print(students[1:4])     # ["Bella", "Connor", "Da-ming"]

The output list does NOT include the element specified in the end index. Personally, I find the easier way to think about slicing is to ‘slice’ up your list between the elements, like slicing a pizza! 🍕🍕🍕

Slicing a list

Try the following questions. Again, don’t skip this! Some of the tips here might come useful, and some of them tricky!

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Question 1

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[2:4])

[13, 14]
Explanation:

This slices out a subset containing the third and fourth element.

Question 2

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[2:3])

[13]
Explanation:

This slices out a subset containing the third element. The result should still be a list (with one element), not an integer!

Question 3

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[:3])

[11, 12, 13]
Explanation:

If you omit the start index, then the list will be sliced from the beginning. So numbers[:3] is equivalent to numbers[0:3].

Question 4

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[3:])

[14, 15, 16]
Explanation:

If you omit the end index, then the list will be sliced from the start index till the end of the list. So numbers[3:] is equivalent to numbers[3:len(numbers)].

Question 5

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[:])

[11, 12, 13, 14, 15, 16]
Explanation:

If you omit both the start and end indices, then the list will be sliced from the beginning till the end of the list. So it will just return the whole list!

Question 6

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[:999])

[11, 12, 13, 14, 15, 16]
Explanation:

Unlike single indices, Python will not return an error if you specify anything beyond the length of the list. It will just 'clip' to the last element.

Question 7

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[100:999])

[]
Explanation:

Again, Python will not return an error if you specify anything beyond the length of the list. In this case, there is nothing between the start and end indices, so Python returns an empty list.

Question 8

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[-2:])

[15, 16]
Explanation:

Negative indices also work in slicing! In this case, it will slice from the second 'partition' from the end of the list (i.e. start index 4), up to the end of the list (since the end index is not specified).

Question 9

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[:-3])

[11, 12, 13]
Explanation:

This time, the list will be sliced from the beginning (because the start index is not specified) to the third 'partition' counting from the end of the list (i.e. index 3 not inclusive).

Question 10

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[-3:-1])

[14, 15]
Explanation:

The list will be sliced from the third 'partition' from the end of the list (i.e. start index 3) to the first 'partition' from the end of the list (i.e. end index 5).

Question 11

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[0:5:2])

[11, 13, 15]
Explanation:

You can provide a third index which represents the 'step' or 'increment'. So in this example, it will slice from index 0, increment the index by 2, and continue up till index 5. This results in the slice 'skipping' one element in between. The default 'step' is 1 when not specified.

Question 12

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[4:2])

[]
Explanation:

This results in an empty list! By default, slicing assumes that the first index will be smaller than the second index.

Question 13

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[4:2:-1])

[15, 14]
Explanation:

The third index can also be negative. In this case, the index will decrease by 1 at each step. Beware - this is also when the pizza 'slicing' way of thinking in terms of partition breaks down a bit. The sliced list starts at numbers[4], numbers[3], and does NOT include numbers[2].

Question 14

What is the output of the following code snippet? Type Error if the result is an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[::-1])

[16, 15, 14, 13, 12, 11]
Explanation:

This is a Python 'idiom' that returns the reverse of a list.