A 2D list (2 Dimensional list) is just a Python list that contains other Python lists. The internal lists can contain any kind of data (although if they contained lists we would have 3D lists, which we do not cover in this course.)
The simplest way to create a 2D list is to assign lists within square brackets to a variable. In general:
some_list = [[v1, v2, ...], ..., [vn-2,
vn-1, ...]]
Some examples:
Code | Description |
---|---|
red_green_blue = [[255, 0, 0], [0, 255, 0], [0, 0, 255]] |
A list of lists of 3 integers |
a_codes = [[65, "A"], [66, "B"], [67, "C"]] |
A list of mixed value lists |
empty = [[]] |
A list with an empty list |
people = [["David", "Lori"], ["Deborah", "Tasmin",
"Tristan"]] |
A list of lists of strings. Note that the internal lists do not all have to have the same length. |
2D list elements can be individually accessed by providing the row and column indexes.
Individual elements of a list can be accessed or changed, as well as an entire internal list. Some examples:
Code | Result | Description |
---|---|---|
|
[0, 255, 0] | 2D List variable references internal list |
|
[['David', 'Lori'], ['Deborah', 'Tasmin', 'Tristan'], ['Benjamin', 'Jake']] | 2D List appends another list |
print(people[1][2]) |
Tristan | Reference the element at row 1, column 2 |
|
[['David', 'Lori'], ['Deb', 'Tasmin', 'Tristan'], ['Benjamin', 'Jake']] | Changes contents of a single element at row 1, column 0 |
|
[[65, 'A'], [70, 'F'], [67, 'C']] | Change the internal list at index 1 |
Lists can be traversed - 'walked' through from one end to the other - in three ways. The following code shows how the contents of the list:
people = [['David', 'Lori'], ['Deb', 'Tasmin', 'Tristan'], ['Benjamin', 'Jake']]
can be traversed:
By index with a while
loop:
rows = len(people)
i = 0
while i < rows:
cols = len(people[i])
j = 0
while j < cols:
print(people[i][j])
j += 1
i += 1
By index with a for
loop:
for i in range(len(people)):
for j in range(len(people[i])):
print(people[i][j])
By value with a for
loop:
for row in people:
for col in row:
print(col)
All three versions print the contents of people
as:
David Lori Deborah Tasmin Tristan Benji Jake
Note that because the lengths of each internal list of people
differ, the internal loops that use an index refer specifically to the
length of each internal list, e.g.
len(people[i])
This is not an issue for the for
loops that loop through the
list values rather than using indexes.
We will look at on traversing a matrix, a rectangular 2D list
where every row has the same number of columns. We can do this only with
indexes - using a for
loop with values won't work, because
such a loop only works row-by-row.
Traversing by column is as simple as swapping the row and column loops in the nested loops. Thus:
rows = len(red_green_blue )
cols = len(red_green_blue [0]) # assumes all rows have same length
j = 0
while j < cols:
i = 0
while i < rows:
print(red_green_blue [i][j]) # [i][j] is unchanged
i += 1
j += 1
or
for j in range(len(red_green_blue[0])):
for i in range(len(red_green_blue)):
print(people[i][j])
A 2D list can be built dynamically. We will look only at building a matrix since that is the simplest example. The following code builds a 3 × 4 matrix containing all zeroes:
rows = 3
cols = 4
matrix = []
for i in range(rows):
row = [] # define a new row
for j in range(cols):
row.append(0) # add value to row
matrix.append(row) # add new row to matrix
The contents of the variable matrix
are:
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]