0% found this document useful (0 votes)
84 views

2D Array in Python

A two dimensional array is an array of arrays that represents a table with rows and columns. It allows data to be accessed using two indices, one for the main array and one for the inner array position. Values can be inserted, updated, and deleted from a two dimensional array using indices and methods like insert() and del().
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

2D Array in Python

A two dimensional array is an array of arrays that represents a table with rows and columns. It allows data to be accessed using two indices, one for the main array and one for the inner array position. Values can be inserted, updated, and deleted from a two dimensional array using indices and methods like insert() and del().
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

2D Array

Two dimensional array is an array within an array. It is an array of arrays. In this type of
array the position of an data element is referred by two indices instead of one. So it represents a
table with rows a columns of data.

Consider the day below of temperature in 4 days.

Day 1 - 11 12 5 2

Day 2 - 15 6 10

Day 3 - 10 8 12 5

Day 4 - 12 15 8 6

The above data can be expressed as two dimensional array

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

2.2.11 Accessing Values in Two Dimensional Array

The data elements in two dimensional arrays can be accessed using two indices. One
index referring to the main or parent array and another index referring to the position of the data
element in the inner array.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

print(T[3])
print(T[1][0])

2.2.12 Inserting Values in Two Dimensional Array

We can insert new data elements at specific position by using the insert() method and
specifying the index.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T.insert(2, [0,5,11,13,6])

for r in T:

for c in r:

print(c,end = " ")

print()

2.2.13 Updating Values in Two Dimensional Array

We can update the entire inner array or some specific data elements of the inner array by
reassigning the values using the array index.

Sample Code: In the code example, The entire array in element two is updated as well as the
specific inner array.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]


T[2] = [11,9]

T[0][3] = 7

for r in T:

for c in r:

print(c,end = " ")

print()

2.2.14 Deleting Values in Two Dimensional Array

The entire inner array or some specific data elements of the inner array can be deleted by
reassigning the values using the del() method with index.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

del T[3]

for r in T:

for c in r:

print(c,end = " ")

print()

You might also like