# 2-Dimensional Arrays in Python for GCSE and A Level Computer Science

2-dimensional arrays are extremely important data structures in programming, and you need to understand them for GCSE and A Level Computer Science. 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.

For the purposes of GCSE and A-Level Computer Science, in Python we refer to arrays as lists. Although there are some technical differences, you don’t need to worry about them at this level.

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 array as an array of arrays. When arranged into a grid, each row represents an array.

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 Array

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 in GCSE Computer Science and A Level Computer Science very often build on each other, much as is Maths. Trying to understand a topic when your understanding of the necessary supporting topics is weak is usually a very bad idea.

So note, for understanding about 2D arrays 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`
• Nested `for` loops
• This example makes use of `tuples` to store the “coodinates”, but that is not always a requirement

## Accessing elements in a 2D array in Python

To access an element from the grid in the code above, you need a double index, which looks like this:

`print(grid)`

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 Arrays 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 Arrays in Python for GCSE and A Level Computer Science is by doing lots of practice. See if you can think of example where you need them. Many games have a 2D board for example, or tables of data.