Python 2d lists programming

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.

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:

Python 2d Lists

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.

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:


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:

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.

Happy computing!

Leave a Repl​​​​​y

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get the code for the classic Retro Alien Blitz Game using Python Turtle Graphics when you join our mailing list