2d arrays in Python
In this post, you will learn about 2d arrays in Python with all basic operations.
An array is a collection of key/value pairs. We can store more than one item in only one variable, i.e., Array. So Array is used when there is a requirement to add more items in a single variable.
As like other programming languages, Python also permits us to make a two-dimensional(2D) array. A two-dimensional (2D) array is utilized to contain different arrays that hold values. The components of a 2D array are stored in column and row wise like a table or matrix. But unlike other programming language, Python does all the array-related operations using the list object.
A list of values is treated like a one-dimensional array and multiple lists within a list is the two dimensional array, where each list is a collection of values, and each list is arranged in a separate row. These are lots of advantages of using a list as an array. In addition to the basic operations, it also supports slicing, negative indexing and much more.
Syntax used to declare an array in Pythonarray_name=[ val1, val2, val3, val4 ]
Consider an example of four highest marks of three subjects -
Phy - [89, 92, 88, 85]
Chm - [75, 73, 70, 68]
Maths - [99, 98, 97, 95]
Here, we have addressed the above data in two dimensional array as -
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
Python Printing Array value
To print out the entire two-dimensional array we can use python for loop as given below. To print out the values in different rows, we use the end of the line in a print statement.
from array import *
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
for m in marks:
for x in m:
print(x,end = " ")
print()
Output of the above code -
89 92 88 85
75 73 70 68
99 98 97 95
Python Accessing Array value
To access the array elements, we will use two indexes, the first index to define the location of the list where our element is stored, and the second index to define the position of the element in that list. If we provide only one index at that point, the entire inner array is printed for that index position. The below example demonstrates how it works.
from array import *
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
print(marks[1])
print(marks[1][0])
print(marks[2][2])
Output of the above code -
[75, 73, 70, 68]
75
97
Python Inserting Values in Two Dimensional Array
Since we have learnt how to create, print elements of an array of two dimensions. Here, we will see how the elements are stored in a 2-d array. Consider if we want to store the top four marks of a biology subject in these 2-d array, then we can use the in-built function insert().
from array import *
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
marks.insert(2, [66, 69, 68, 67])
for r in marks:
for x in r:
print(x,end = " ")
print()
Output of the above code -
89 92 88 85
75 73 70 68
66 69 68 67
99 98 97 95
Python Updating Values in Two Dimensional Array
Updatation is the process of updating an existing element of an array at a given index. We can update the entire inner array or some specific data elements of the inner array by reassigning the values using the array index.
from array import *
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
marks.insert(2, [66, 69, 68, 67])
marks[2] = [98,96]
for r in marks:
for x in r:
print(x,end = " ")
print()
Output of the above code -
89 92 88 85
75 73 70 68
98 96
99 98 97 95
Python Deleting Values in Two Dimensional Array
We can delete some specific data elements of the inner array or the entire inner array by reassigning the values using the del() method with index.
from array import *
marks = [[89, 92, 88, 85], [75, 73, 70, 68], [99, 98, 97, 95]]
del marks[1]
for r in marks:
for x in r:
print(x,end = " ")
print()
Output of the above code -
89 92 88 85
99 98 97 95
Related Articles
Merge sort program in Python
Calculate a Percentage in Python
Multiply all elements in list Python
Convert array to list Python
Python Numpy Array Shape
Python heapq example
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Text Detection and Extraction using OpenCV and OCR
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML