Chapter 2: Lists

Nested List

face Josiah Wang

An item in a list can be any object. It can even be another list!!

nested_list = [1, 2, [3, 4, 5], 6]
super_nested_list = [1, 2, [3, 4, [5, 6, 7], 8], 9, [10, 11]]

Such nested lists can be used in many different applications. A prime example is for representing a table or a matrix. The elements in the ‘outer’ list represents each row, and the ‘inner’ list represents the columns in each row.

Matrix as a nested list

Accessing the elements inside a list is intuitive. The example below should be self-explanatory, I hope!

>>> x = [1, 2, [3, 4, 5], 6]
>>> print(len(x))
4
>>> print(x[2])
[3, 4, 5]
>>> print(len(x[2]))
3
>>> print(x[2][1])
4

Let’s check whether you really got this!

1 2 3 4 5 6 7 8 9 10

Question 1

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

numbers = [[1, 2], [3, 4], [5, 6], [7, 8]]
print(len(numbers))

4
Explanation:

There are four elements in the outer list.

Question 2

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(len(numbers[1]))

3
Explanation:

There are three elements in the inner list of the outer element (index 1), i.e. [2, 3, 4].

Question 3

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[2])

[5, 6]
Explanation:

The element at index 2 in the outer list is [5, 6].

Question 4

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[3][2])

9
Explanation:

We need the value of the outer element at index 3 ([7, 8, 9, 10]), and the element at index 2 of this inner list.

Question 5

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[1][3])

Error
Explanation:

There are only 3 elements in numbers[1], so you cannot access numbers[1][3].

Question 6

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[-1][1])

8
Explanation:

Negative indices work as usual. Take the last element in the list (numbers[-1]) which is [7, 8, 9, 10], and the element at position 1 inside this list.

Question 7

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

numbers = [[1], [2, 3, 4], [5, 6], [7, [8, 9], 10]]
print(numbers[3][1])

[8,9]
Explanation:

Here we have a list inside a list inside a list! Element numbers[3] is [7, [8, 9], 10], and numbers[3][1] is [8, 9]

Question 8

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

numbers = [[1], [2, 3, 4], [5, 6], [7, [8, 9], 10]]
print(numbers[3][1][0])

8
Explanation:

Element numbers[3] is [7, [8, 9], 10], element numbers[3][1] is [8, 9], and numbers[3][1][0] is 8.

Question 9

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[2:4])

[[5, 6], [7, 8, 9, 10]]
Explanation:

Slicing still works with nested lists!

Question 10

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

numbers = [[1], [2, 3, 4], [5, 6], [7, 8, 9, 10]]
print(numbers[2:4][0])

[5, 6]
Explanation:

A bit confusing. numbers[2:4] results in the sublist [[5, 6], [7, 8, 9, 10]]. The first element of that list (index 0) is [5, 6].