Introduction to 2D Arrays In Python
Arrangement of elements that consists of making an array, i.e. an array of arrays within an array. A type of array in which two indices refer to the position of a data element as against just one, and the entire representation of the elements looks like a table with data being arranged as rows and columns, and it can be effectively used for performing from simplest operations like addition, subtraction to toughest tasks like multiplication and inverse operations. In the context of data analysis, based on the requirement, they are termed as two-dimensional arrays in the Python programming language.
Let us take an example, where we have to measure the height and weight of 4 people.
Person 1: 6.0 ft 61 kg
Person 2: 5.3 ft 53 kg
Person 3: 5.9 ft 67 kg
Person 4: 6.2 ft 63 kg
So the above set of data can be represented with the help of a two-dimensional array in the following way.
A= [[6, 61], [5.3, 53], [5.9, 67], [6.2, 63]]
Different operations in 2D arrays in Python
Here we Explain Different Operations in 2D arrays in Python along with Examples.
Creating an array
Let us see how we can create a 2D array in Python
Method 1 – Here, we are not defining the size of rows and columns and directly assigning an array to some variable A.
A = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for i in A:
for j in i:
print(j,end = " ")
Method 2 – Here, we will be defining the size of the array and then will try to do some basic operations and then we will print our array.
rows, cols = (5, 5)
arr = [*cols]*rows
Method 3 – In this method, we will be asking the users input to know the number of rows and columns; we can use the input method for taking the user’s input.
row = int(input("Input the number of rows: "))
col = int(input("Input the number of columns: "))
list = [[0 for col in range(col)] for row in range(row)] for row in range(row):
for col in range(col):
Inserting elements to an array
Here, we have defined an array with the name “cars”, and as in the first line of the code, the elements of the array are Ford, Volvo, and BMW. Now suppose, if we would like to add more elements to the array, we can make use of the append function. In the third line of the code, we have used the append function to add another car element, “Bugatti”, to the existing array. Then we have printed the array.
cars = ["Ford", "Volvo", "BMW"]
Well, what if we would want to add several elements at a time to the array and not just one?
In that scenario, we can make use of the extend function.
cars = ["Ford", "Volvo", "BMW"]
As we can see here, we have used extend function to add multiple elements to the array at once, and then we have printed our array. It is also possible to concatenate to different arrays.
cars1 = ["Ford", "Volvo", "BMW"]
car = cars1 + cars2
Here, we have defined two different arrays with name cars1 and cars2, and we have then added these two arrays and stored inside an array called the car, then we have simply printed the car array. The final result has the elements from both the arrays.
Update/Changing array elements
In this section, we will try to update and change the elements of the array. Arrays are mutable and the elements of an array can be changed. Below is an example of how we can do this
import array as arr num = arr.array(‘i’, [1, 2, 3, 4, 5, 6])# changing the first element
# changing second to the fourth elementnum[1print(num)
As we see, we have first created an array called “num”. We have replaced the first element of the array with the number 10, and then we have printed the array. Next, we have changed the array elements from the second position to the fourth position, and then we have printed it.
Accessing the array elements
We can access elements of the array by specifying the index position. In the below example, we have created an array of numbers, and then we have printed the very first element of the array by specifying the index position with square braces of num array. The index in an array starts at 0, and it increments by 1 as we go through. We can also directly access the last element of the array by specifying the index as -1 (minus 1).
import array as arr
num = arr.array('i', [1, 2, 3, 4])
print("First element:", num)
print("Second element:", num)
print("Last element:", num[-1])
Removing Array Elements
We can remove elements from the array by making use of the del function and specifying the index position for which we would like to delete the array element.
import array as arr
num = arr.array('i', [2, 3, 4, 5, 6])
del num # removing the fourth element
In this section, we have learned different operations that can be performed on an array. We have started with created an array and saw different ways to create an array; then we saw how we could add an element to the array, how to change or update elements of an array, how to access the elements of an array, and finally, we learned how to remove the array elements or how to delete the entire array itself.
This has been a guide to 2D Arrays In Python. Here we discuss Different operations in 2D arrays in Python along with Method, Code, and Output. You can also go through our other suggested articles to learn more –