Lists within Lists
Earlier we learned that lists can store values of any data type. Does this mean we can store lists inside of lists? Well, yes we can…
A multi-dimensional list is a list of lists, meaning that the values inside the list are also lists.
A nested list is a list that appears as an element inside another list. Nested lists can store strings, numbers, and even more lists.
multidim_list = ['a', 'b', [1, 2, 3], 'rutabaga']
In multidim_list
, the element at index 2 is a nested list. If we
print(multidim_list[2])
, we see [1, 2, 3]
appear in the console.
The figure below shows a courses
list that holds lists at each index
position. Each nested list contains classes from the same subject area.
Notice that each nested list has its own set of index values.
Two Dimensional Lists
The simplest form of a multi-dimensional list is a two dimensional list. Each element is a nested list, which contains multiple data values.
two_dim_list = [['a', 'b', 'c'], [90, 101], [True, False, False, True]]
print(len(two_dim_list))
Console Output
3
two_dim_list
holds three elements, each of which is a list. Note that the
len()
function only counts these three elements and NOT the total number of
items inside of the nested lists.
To access the values from a nested list, use two sets of square brackets and two index values. The indexes evaluate from left to right. The first index selects one of the nested lists, and the second index selects an element from that nested list.
Use one set of brackets to access a nested list, and add a second set of brackets to access the values inside that list.
|
|
Console Output
['a', 'b', 'c']
[90, 101]
[True, False, False, True]
c
101
True
Applying Methods to Nested Lists
We can apply list methods to either the nested or outer lists. However, we must use bracket notation carefully.
To apply a method to the outer list, the syntax is:
list_name.method()
To apply a method to one of the nested lists, the syntax is:
list_name[index_of_nested_list].method()
Examine how including bracket notation affects how the reverse
method
changes each list.
|
|
Console Output
[ ['A', 'B', 'C'], ['A', 'b', 'c'], ['a', 'b', 'c'] ]
[ ['a', 'b', 'c'], ['A', 'b', 'c'], ['C', 'B', 'A'] ]
Beyond Two Dimensional lists
There is no limit to how many layers we can have inside our lists. However, it is rare to use more than two dimensions.
Check Your Understanding
Use the following list to answer the questions:
|
|
Which of the following will access the name "Jones"
?
data[0][0]
data[0][1]
data[1][0]
data[1][1]
How would you add "dance"
to the first nested list?
data.append('dance')
data[0].append('dance')
data[1].append('dance')