# Two Dimensional Lists

A two dimensional array is an array within an array.

It is an array of arrays.

The position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data. In the below example of a two dimensional array, observer that each array element itself is also an array.

Consider the example of recording temperatures 4 times a day, every day. Some times the recording instrument may be faulty and we fail to record data. Such data for 4 days can be presented as a two dimensional array as below.

```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 represented as a two dimensional array as below.

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

## Accessing Values in a Two Dimensional Array

The data elements in two dimesnional 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. If we mention only one index then the entire inner array is printed for that index position. The example below illustrates how it works.

```from array import *

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

print(T)print(T)

print(T)```

When the above code is executed, it produces the following result −

`[[11, 12, 5, 2], [15, 6, 10], [10, 8, 12, 5], [12, 15, 8, 6]]`
`[11, 12, 5, 2] `
`10`

To print out the entire two dimensional array in a good format we can use python for loop as shown below. We use end of line to print out the values in different rows.

```from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
for c in r:
print(c,end = " ")
print()```

When the above code is executed, it produces the following result −

```11 12 5 2
15 6 10
10 8 12 5
12 15 8 6
```

## Inserting Values in Two Dimensional Array

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

In the below example a new data element is inserted at index position 2.

```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()```

When the above code is executed, it produces the following result −

```11 12 5 2
15 6 10
0 5 11 13 6
10 8 12 5
12 15 8 6
```

## 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.

```from array import *

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

T = [11,9]
T = 7
for r in T:
for c in r:
print(c,end = " ")
print()```

When the above code is executed, it produces the following result −

```11 12 5 7
15 6 10
11 9
12 15 8 6
```

## Appending Values into Two Dimensional Array

We can append a value at the end of a list. Here we append to the end of the second list (T):

```T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
T.append(4)
for r in T:
for c in r:
print(c, end = " ")
print()
```

Next we take the list T and add a list to the end of the list of lists in T:

```T.append([4,3,6,5])
for r in T:
for c in r:
print(c, end = " ")
print()
```

The result of the last to operations is:

```11 12 5 2
15 6 10 4
10 8 12 5
12 15 8 6

11 12 5 2
15 6 10 4
10 8 12 5
12 15 8 6
4 3 6 5
```

## Deleting the Values in Two Dimensional Array

We can delete the entire inner array or some specific data elements of the inner array by reassigning the values using the del() method with index. But in case you need to remove specific data elements in one of the inner arrays, then use the update process described above.

```from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

del T

for r in T:
for c in r:
print(c,end = " ")
print()```

When the above code is executed, it produces the following result −

```11 12 5 2
15 6 10
10 8 12 5 ```
Topic revision: r3 - 2019-03-19 - JimSkon    Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback