Updated June 29, 2023
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. It can effectively perform the simplest operations like addition and subtraction to the toughest tasks like multiplication and inverse operations. Based on the requirement, they are termed two-dimensional arrays in the Python programming language in the context of data analysis.
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 data set 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 and Examples.
- Create
- Insert
- Update
- Delete
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.
Code:
A = [[56, 64, 67, 24], [77, 25, 52], [84, 27, 83, 95], [78, 26, 38, 93]]
for i in A:
for j in i:
print(j,end = " ")
print()
Output:
Method #2
Here, we will define the size of the array and then will try to do some basic operations, then print our array.
Code:
rows, cols = (4, 4)
arr = [[0]*cols]*rows
print(arr)
Output:
Method #3
In this method, we will ask the user input to know the number of rows and columns; we can use the input method to take the user’s input.
Code:
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):
list[row][col]= row*col
print(list)
Output:
Inserting elements into an array
Here, we have defined an array with the name “Shoes”, and as in the first line of the code, the elements of the array are Airforce, Adidas, and Gucci. If we want to add more elements to the array, we can use the append function. In the third line of the code, we have used the append function to add another car element, “Louis Vuitton”, to the existing array. Then we printed the array.
Code:
Shoes = ["Airforce", "Adidas", "Gucci"]
print(Shoes)
Shoes.append("Louis Vuitton")
print(Shoes)
Output:
Well, what if we want to add several elements at a time to the array, not just one?
In that scenario, we can make use of the extend function.
Code:
Shoes = ["Airforce", "Adidas", "Gucci"]
print(Shoes)
Shoes.append("Louis Vuitton")
print(Shoes)
Shoes.extend(["Puma", "Reebok"])
print(Shoes)
Output:
As we can see here, we used extend function to add multiple elements to the array at once, and then we printed our array. It is also possible to concatenate to different arrays.
Code:
Shoes1 = ["Airforce", "Adidas", "Gucci"]
Shoes2 = ["Puma", "Reebok"]
Shoes = Shoes1 + Shoes2
print(Shoes)
Here, we have defined two different arrays with the names Shoes1 and Shoes2, and we have then added these two arrays and stored them inside an array called the Shoes, then we have printed the Shoe array. The final result has the elements from both 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.
Code:
import array as arr
num = arr.array('i', [5, 2, 7, 1, 9, 3])
# changing the first element
num[0] = 10
print(num)
# changing second to the fourth element
num[1:4] = arr.array('i', [4, 6, 8])
print(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 changed the array elements from the second position to the fourth position, and then we printed it.
Accessing the array elements
We can access elements of the array by specifying the index position. In the example below, we created an array of numbers, then printed the first element by selecting the index position with square braces of the num array. The index in an array starts at 0 and increments by one as we go through. We can also directly access the last element of the array by specifying the index as -1 (minus 1).
Code:
import array as arr
num = arr.array('i', [1, 2, 3, 4])
print("First element:", num[0])
print("Second element:", num[1])
print("Last element:", num[-1])
Output:
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.
For example,
Code:
import array as arr
num = arr.array('i', [2, 3, 4, 5, 6])
del num[3] # removing the fourth element
print(num)
Output:
It is also possible to delete the entire array by just giving the array name without any index position. For example:
Code:
import array as arr
num = arr.array('i', [2, 3, 4, 5, 6])
del num[3] # removing the fourth element
print(num)
del num
print(num)
This will delete the entire array, and when you try to print the array, it will give an error that the array does not exist.
Conclusion
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. We saw how we could add an element to the array, how to change or update elements of an array, and how to access the elements of an array. Finally, we learned how to remove the array elements or delete the entire array.
Recommended Article
We hope that this EDUCBA information on “2D Arrays In Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.