10.6. Nested Lists¶
A nested list is a list that appears as an element in another list. For example:
1
nested: [[int]]
2
inner_list: [int]
3
item: int
4
5
nested = [[3, 87], [12], [37, 33, 24], [10, 20]]
6
inner_list = nested[3]
7
print(str(inner_list))
8
item = inner_list[1]
9
print(str(item))
10
print(str(nested[3][1]))
11
(chp09_nest_list)
The list nested
is a lists of lists. Each inner list can be a
different length, but they all must be lists of ints. If we print
nested[3]
, we get [10, 20]
. To extract an element from the
nested list, we can proceed in two steps. First, extract the inner
list, then extract the item of interest. It is also possible to
combine those steps using bracket operators that evaluate from left to
right.
Check your understanding
list-23-1: What is printed by the following statements?
a_list: [[int]]
a_list = [[4, 6, 8], [777, 888, 999]]
if a_list[0][1] < 100:
print(str(a_list[1][0]))
else:
print(str(a_list[1][1]))
You have attempted 1 of 3 activities on this page