Two-Dimensional Arrays in Python

A two-dimensional array is an array containing arrays. They are used to record grouped data for example if we want to store the scores of all players in a series, we can store them in different arrays and put those arrays in one array to access easily. 

So if we have the following data:

Player 1 - [45, 60, 36, 49]
Player 2 - [25, 45, 44, 78]
Player 3 - [22, 12, 100, 55]
Player 4 - [98, 29, 81, 50]

We can make an array of scores as follows:

scores = [[45, 60, 36, 49], [25, 45, 44, 78], [22, 12, 100, 55], [98, 29, 81, 50]]

You can think of these arrays in terms of columns and rows in which the columns contain cores of columns while rows represent different matches in our example.

Note: Since the array module of Python is new, Python handles two-dimensional arrays using the list type.

Creating Python Two-Dimensional Arrays

To create a two-dimensional array using the above data you just have to put it in a variable.

scores = [[45, 60, 36, 49], [25, 45, 44, 78], [22, 12, 100, 55], [98, 29, 81, 50]]
print(scores)

Simple printing the array will print it as below:

[[45, 60, 36, 49], [25, 45, 44, 78], [22, 12, 100, 55], [98, 29, 81, 50]]

If you want to print only the elements of the array, you can use two nested for loops:

for row in scores:
 for column in row:
    print(column + “ ”)
  print()

The output will be:

45 60 36 49
25 45 44 78
22 12 100 55
98 29 81 50

Indexing Python Two-Dimensional Arrays

To access the elements of a two-dimensional array, indexing is done using two square brackets [][], the first pair for row value and the second for column value. Considering the above array, we can print its different elements as:

print("First player, first match: ", scores[0][0])
print("Second player, third match: ", scores[1][2])
print("Third player, last match: ", scores[2][-1])
print("Last player, first match: ", scores[-1][0])
First player, first match: 45
Second player, third match: 44
Third player, last match: 55
Last player, first match: 98

Adding elements to Python Arrays

We can add elements (arrays) to arrays using insert method which takes the index and the element as the arguments.

scores.insert(1, [2, 65, 78, 7])
print(scores)
[[45, 60, 36, 49], [2, 65, 78, 7], [25, 45, 44, 78], [22, 12, 100, 55], [98, 29, 81, 50]]

Updating elements in Python Arrays

Elements of an array can be updated just by assigning new values to the same index using the assignment operator in Python ‘=’:

scores[3][-1] = 100
print(scores)
[[45, 60, 36, 49], [2, 65, 78, 7], [25, 45, 44, 100], [22, 12, 100, 55], [98, 29, 81, 50]]

Deleting elements in Python Arrays

del keyword of Python can be used to delete an element of an array by specifying its index:

del scores[3]
print(scores)

del scores[0][2]
[[45, 60, 36, 49], [2, 65, 78, 7], [25, 45, 44, 100], [98, 29, 81, 50]]

[[45, 36, 49], [2, 65, 78, 7], [25, 45, 44, 100], [98, 29, 81, 50]]

So that’s it, folks, now you know the basics of two-dimensional arrays and you can start storing matrix data this way and make your programs more efficient. Feel free to comment below in case of any queries.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest