How to Implement 2D Array in Python

How to implement 2d array in python : In this article we will see how to create a two-dimensional array in python.
Overview
In python, a two-dimensional array is an array that is inside another array. We can call it an array of arrays. To identify the position of an element in this kind of two-dimensional array, we need to specify two index instead of one (the first index corresponds to the row of the array and the second index to the column). The easiest way to create this type of table is to use nested lists.
So we will see after this tutorial how to create a nested python array.
Nested List
To create our 2-dimensional table, we can use nested lists. In python, a list is a variable in which we can put several variables. We will reproduce the table below using these kinds of lists :
Index (rows/columns) | 0 | 1 | 2 | 3 | 4 |
0 | 1000 | 2000 | 3000 | 4000 | 5000 |
1 | 1100 | 2100 | 3100 | 4100 | 5100 |
2 | 1200 | 2200 | 3200 | 4200 | 5200 |
3 | 1300 | 2300 | 3300 | 4300 | 5300 |
4 | 1400 | 2400 | 3400 | 4400 | 5400 |
To represent this array as a two-dimensional table, here’s how to define our nested list :
def print_array(arr):
for r in arr:
for c in r:
print(c, end=" ")
print()
array2d = [[1000, 2000, 3000, 4000, 5000],
[1100, 2100, 3100, 4100, 5100],
[1200, 2200, 3200, 4200, 5200],
[1300, 2300, 3300, 4300, 5300],
[1400, 2400, 3400, 4400, 5400],
]
print_array(array2d)
Output:
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 3200 4200 5200
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
The print_array() function displays the array cleanly in two dimensions. As you can see we have created nested lists to create our array. Its use is very simple.
Accessing Values in a 2D array
To display a single cell, row or column we will need the table hints. Here is an example for each case:
array2d = [[1000, 2000, 3000, 4000, 5000],
[1100, 2100, 3100, 4100, 5100],
[1200, 2200, 3200, 4200, 5200],
[1300, 2300, 3300, 4300, 5300],
[1400, 2400, 3400, 4400, 5400],
]
# print element 3100
print(array2d[1][2])
# print row 3
print(array2d[2])
# print colunm 4
print([i[3] for i in array2d])
Output :
3100
[1200, 2200, 3200, 4200, 5200]
[4000, 4100, 4200, 4300, 4400]
To display column 4 of a two-dimensional table, you have to scroll through the 4th element of each row. The syntax is more complex to grasp but remains very powerful when you need to browse a column.
Note : The first index of a list is always 0 and not 1. So be very careful when you want to retrieve some elements of an array, at the beginning it often happens that you make a mistake 🙂
Updating Values in a 2D array
In a table we can update any element of the table (be it a cell or a row) :
array2d = [[1000, 2000, 3000, 4000, 5000],
[1100, 2100, 3100, 4100, 5100],
[1200, 2200, 3200, 4200, 5200],
[1300, 2300, 3300, 4300, 5300],
[1400, 2400, 3400, 4400, 5400],
]
print_array(array2d)
# Update value in array2d[1][2]
array2d[1][2] = "AMIRADATA"
# Update first row
array2d[0] = [1,2,3]
print("Array Updated")
print_array(array2d)
Output :
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 3200 4200 5200
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
Array Updated
1 2 3
1100 2100 AMIRADATA 4100 5100
1200 2200 3200 4200 5200
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
Each line of the table does not necessarily have the same length, we were able to modify the first line (which contained 5 elements) with only a list of 3 elements. The table can also contain different types. This is one of the properties of python lists.
Inserting Values in a 2D array
It is also possible to add new rows to our table by using the insert() function:
def print_array(arr):
for r in arr:
for c in r:
print(c, end=" ")
print()
array2d = [[1000, 2000, 3000, 4000, 5000],
[1100, 2100, 3100, 4100, 5100],
[1200, 2200, 3200, 4200, 5200],
[1300, 2300, 3300, 4300, 5300],
[1400, 2400, 3400, 4400, 5400],
]
print_array(array2d)
array2d.insert(3, ["the", "row", "has", "been", "inserted"])
print("\nArray Updated\n")
print_array(array2d)
Output:
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 3200 4200 5200
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
Array Updated
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 3200 4200 5200
the row has been inserted
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
Deleting Values in a 2D array
You can remove an element or row from the array by using the del() function:
array2d = [[1000, 2000, 3000, 4000, 5000],
[1100, 2100, 3100, 4100, 5100],
[1200, 2200, 3200, 4200, 5200],
[1300, 2300, 3300, 4300, 5300],
[1400, 2400, 3400, 4400, 5400],
]
print_array(array2d)
# delete row 4
del array2d[3]
# delete element [2][2]
del array2d[2][2]
print("\nArray Updated\n")
print_array(array2d)
Output:
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 3200 4200 5200
1300 2300 3300 4300 5300
1400 2400 3400 4400 5400
Array Updated
1000 2000 3000 4000 5000
1100 2100 3100 4100 5100
1200 2200 4200 5200
1400 2400 3400 4400 5400
Conclusion
As you have seen, it is quite simple to create a two-dimensional python array. There are alternatives to the nested list using, for example, the Numpy library. We will try to detail this other method in a future article.
Comments
Leave a comment