Python 2D array
An array is a collection of linear data structures that contain all elements of the same data type in contiguous memory space. It is like a container that holds a certain number of elements that have the same data type. An array's index starts at 0, and therefore, the programmer can easily obtain the position of each element and perform various 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 form like rows and columns. In this array, the position of data elements is defined with two indices instead of a single index.
Array_name = [rows][columns] # declaration of 2D array Arr-name = [ [m1, m2, m3, … . mn], [n1, n2, n3, … .. nn] ]
Where m is the row and n is the column of the table.
Access Two-Dimensional Array
In Python, we can access elements of a two-dimensional array using two indices. The first index refers to the indexing of the list and the second index refers to the position of the elements. If we define only one index with an array name, it returns all the elements of 2-dimensional stored in the array.
Let's create a simple program to understand 2D (two dimensional) arrays in Python.
In the above example, we passed 1, 0, and 2 as parameters into 2D array that prints the entire row of the defined index. And we have also passed student_dt that represents the 3rd index and 4th position of a 2-dimensional array of elements to print a particular element.
Traversing the element in 2D (two dimensional)
Insert elements in a 2D (Two Dimensional) Array
We can insert elements into a 2 D array using the insert() function that specifies the element' index number and location to be inserted.
Update elements in a 2 -D (Two Dimensional) Array
In a 2D array, the existing value of the array can be updated with a new value. In this method, we can change the particular value as well as the entire index of the array. Let's understand with an example of a 2D array, as shown below.
Create a program to update the existing value of a 2D array in Python.
Delete values from a 2D (two Dimensional) array in Python
In a 2- D array, we can remove the particular element or entire index of the array using del() function in Python. Let's understand an example to delete an element.
Size of a 2D array
A len() function is used to get the length of a two-dimensional array. In other words, we can say that a len() function determines the total index available in 2-dimensional arrays.
Let's understand the len() function to get the size of a 2-dimensional array in Python.
Write a program to print the sum of the 2-dimensional arrays in Python.