An array is a collection of linear data buildings that comprise all factors of the same facts type in contiguous reminiscence space. It is like a container that holds a positive number of elements that have the same records type. An array’s index starts at 0, and therefore, the programmer can without problems attain the role of each issue and function more than a few operations on the array. In this section, we will learn about 2D (two dimensional) arrays in Python.

Two-Dimensional Array (2D Array)

A 2D array is an array of arrays that can be represented in matrix shape like rows and columns. In this array, the role of statistics factors is described with two indices rather of a single index.

Syntax

```
Student_dt = [ [72, 85, 87, 90, 69], [80, 87, 65, 89, 85], [96, 91, 70, 78, 97], [90, 93, 91, 90, 94], [57, 89, 82, 69, 60] ]
#print(student_dt[])
print(Student_dt[1]) # print all elements of index 1
print(Student_dt[0]) # print all elements of index 0
print(Student_dt[2]) # print all elements of index 2
print(Student_dt[3][4]) # it defines the 3rd index and 4 position of the data element.
```

Output:

In the above example, we passed 1, 0, and 2 as parameters into 2D array that prints the entire row of the described index. And we have also handed student_dt[3][4] that represents the 3rd index and 4th function of a 2-dimensional array of elements to print a precise element.

Traversing the element in 2D (two dimensional)

Program.py

```
# write a program to traverse every element of the two-dimensional array in Python.
Student_dt = [ [72, 85, 87, 90, 69], [80, 87, 65, 89, 85], [96, 91, 70, 78, 97], [90, 93, 91, 90, 94], [57, 89, 82, 69, 60] ]
# Use for loop to print the entire elements of the two dimensional array.
for x in Student_dt: # outer loop
for i in x: # inner loop
print(i, end = " ") # print the elements
print()
```

Output:

Insert elements in a 2D (Two Dimensional) Array

We can insert factors into a 2 D array using the insert() characteristic that specifies the element’ index number and place to be inserted.

Insert.py

```
# Write a program to insert the element into the 2D (two dimensional) array of Python.
from array import * # import all package related to the array.
arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements.
print("Before inserting the array elements: ")
print(arr1) # print the arr1 elements.
# Use the insert() function to insert the element that contains two parameters.
arr1.insert(1, [5, 6, 7, 8]) # first parameter defines the index no., and second parameter defines the elements
print("After inserting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
```

Output:

Update elements in a 2 -D (Two Dimensional) Array

In a 2D array, the current price of the array can be updated with a new value. In this method, we can alternate the particular fee as nicely as the complete index of the array. Let’s recognize with an example of a 2D array, as proven below.

Create a program to replace the existing fee of a 2D array in Python.

Update.py

```
from array import * # import all package related to the array.
arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements.
print("Before inserting the array elements: ")
print(arr1) # print the arr1 elements.
arr1[0] = [2, 2, 3, 3] # update the value of the index 0
arr1[1][2] = 99 # define the index [1] and position [2] of the array element to update the value.
print("After inserting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
```

Output:

Delete values from a 2D (two Dimensional) array in Python

In a 2- D array, we can take away the unique aspect or entire index of the array the use of del() function in Python. Let’s recognize an instance to delete an element.

Delete.py

```
from array import * # import all package related to the array.
arr1 = [[1, 2, 3, 4], [8, 9, 10, 12]] # initialize the array elements.
print("Before Deleting the array elements: ")
print(arr1) # print the arr1 elements.
del(arr1[0][2]) # delete the particular element of the array.
del(arr1[1]) # delete the index 1 of the 2-D array.
print("After Deleting the array elements ")
for i in arr1: # Outer loop
for j in i: # inner loop
print(j, end = " ") # print inserted elements.
print()
```

Output:

Size of a 2D array

A len() function is used to get the size of a two-dimensional array. In other words, we can say that a len() characteristic determines the whole index reachable in 2-dimensional arrays.

Let’s recognize the len() characteristic to get the measurement of a 2-dimensional array in Python.

Size.py

```
array_size = [[1, 3, 2],[2,5,7,9], [2,4,5,6]] # It has 3 index
print("The size of two dimensional array is : ")
print(len(array_size)) # it returns 3
array_def = [[1, 3, 2], [2, 4, 5, 6]] # It has 2 index
print("The size of two dimensional array is : ")
print(len(array_def)) # it returns 2
```

Output:

Write a application to print the sum of the 2-dimensional arrays in Python.

Matrix.py

```
def two_d_matrix(m, n): # define the function
Outp = [] # initially output matrix is empty
for i in range(m): # iterate to the end of rows
row = []
for j in range(n): # j iterate to the end of column
num = int(input(f "Enter the matrix [{0}][{j}]"))
row.append(num) # add the user element to the end of the row
Outp.append(row) # append the row to the output matrix
return Outp
def sum(A, B): # define sum() function to add the matrix.
output = [] # initially, it is empty.
print("Sum of the matrix is :")
for i in range(len(A)): # no. of rows
row = []
for j in range(len(A[0])): # no. of columns
row.append(A[i][j] + B[i][j]) # add matrix A and B
output.append(row)
return output # return the sum of both matrix
m = int(input("Enter the value of m or Row\n")) # take the rows
n = int(input("Enter the value of n or columns\n")) # take the columns
print("Enter the First matrix ") # print the first matrix
A = two_d_matrix(m, n) # call the matrix function
print("display the first (A) matrix")
print(A) # print the matrix
print("Enter the Second (B) matrix ")
B = two_d_matrix(m, n) # call the matrix function
print("display the Second (B) matrix")
print(B) # print the B matrix
s= sum(A, B) # call the sum function
print(s) # print the sum of A and B matrix.
```

Output:

Where m is the row and n is the column of the table.

Access Two-Dimensional Array

In Python, we can get right of entry to factors of a two-dimensional array the usage of two indices. The first index refers to the indexing of the list and the 2nd index refers to the position of the elements. If we outline only one index with an array name, it returns all the elements of 2-dimensional saved in the array.

Let’s create a easy software to understand 2D (two dimensional) arrays in Python.

2dSimple.py

## Leave a Review