Arrays in Python

A Python array is a little bit different to arrays in other programming languages in that it uses something called 'list' instead of array. Python 'lists' offer more flexibility than arrays as they can contain different types of data and their length can be varied.

There are advantages and disadvantages of using both types. It is often convenient to use a list where you do not need to know the data type or length. However, arrays are a more direct way of accessing data in memory so they are much faster and more efficient.

Programming languages like C and Java offer the choice of both array and list as a data type.

Graphic of a bowling alley used to illustrate arrays indexes

To create a two-dimensional nine-by-nine array in Python, we would use code such as:

game= [[0 for x in range (9)] for y in range(9)]

This statement contains two FOR loops and each element is initialised in turn.

We can now use statements like the following to add data to positions in the array:

game[0][0] = '3' game[0][1] = '3' game[0][2] = '3' game[1][0] = '2' game[1][1] = '2'

Our array would now look like this:

An array table