2-dimensional lists are an extremely important data structure in Python programming, with many applications. They can be very confusing to begin with, and you should make sure you are confident and competent with 1-dimensional arrays before learning about them.
Applications of 2d Lists is Python
- Representing grids e.g. pixels
- Game boards
- Matrices for mathematical applications
- Representing tabular data, like in a spreadsheet
- Storing and accessing data from scientific experiments
- Accessing DOM elements for web development
- Understanding pandas dataframes
- Paving the way for higher-dimensional lists
If you are interested in a deep-dive into Python 2d Lists, I have a course on LinkedIn Learning called Learning Python 2d Lists: A Game-based Project which you might want to check out.
Please note this is an affiliate link meaning I will receive a commission if you make a purchase after clicking it.
Understanding 2d Lists in Python
In order to not get lost when using 2D arrays in Python, you need to fix your orientation, much like when reading a map. You should always be clear about whether a particular index is referring to a row or a column.
You can think of a 2D list as a list of lists. When arranged into a grid, each row represents a single list.
Look at this image:
It gives you the coordinates of the items in a grid in terms of a row number followed by a column number. It is is important to note that the order of these in terms of vertical and horizontal is different to what you are used to from x,y coordinates in Maths. These 2D lists do exist in Maths, and are called matrices, but that is not a topic you are likely to cover unless you study A Level Maths.
Python code for a 2D List
The code which produces the grid in the picture is below.
grid = []
for row in range(5):
new_row = []
for col in range(5):
new_row.append((row, col))
grid.append(new_row)
for row in grid:
print(row)
Study this code well, and make sure you understand it fully. It makes use of nested for loops, which you will also need to be familiar with.
In general, topics Python programming and Computer Science very often build on each other. Trying to understand a topic when your understanding of the necessary supporting topics is weak is usually a bad idea.
So note, for understanding about 2D Lists in Python you need to be comfortable with
- Simple 1-dimensional lists
- Accessing elements of a 1D list using an index. E.g.
my_list[3]
- Nested
for
loops - This example makes use of
tuples
to store the “coordinates”, but that is not always a requirement
Accessing elements in a 2D List in Python
To access an element from the grid in the code above, you need a double index, which looks like this:
print(grid[3][1])
Note that print(grid[3,1])
will lead to a TypeError
, even though it might seem to make sense.
Here’s one more example to help you to get familiar with 2D Lists in Python:
matrix = []
for row in range(1, 5):
new_row = []
for col in range(1,5):
new_row.append(row * col)
matrix.append(new_row)
for row in matrix:
for el in row:
print(el, end ="\t") # \t means "tab"
print()
What do you think the output will be for this code?
The only way to get good at 2D Lists in Python is by doing lots of practice. See if you can think of example where you need them, and then go ahead and write some code. Many games have a 2D board for example, or tables of data. The more time you spend programming in Python, the more often you will come across situations where your knowledge of 2d list will come in very handy.
It is worth noting that the interpretation of a 2d list in row/column form is for human convenience only. Internally the data is not stored like this. So for example you can access [[2,0],[3,5],[7,9],[5,4]]
via [i][j]
where i
is 0
to 3
and j
is 0
to 1
without thinking in terms of a table. However if you are going to conceptualise a 2d list as a table, there is a widely used convention of the first index representing the row and the second index representing the column. I strongly advise students to stick to this convention even though it may seem unfamiliar at first. It will make reasoning about your data easier later on in your studies.
Happy computing!