# Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of different types and is mutable. A nested list is a list that has another list as a data item in it. These are mostly used to do operations on matrices. A whole other list will occupy a single slot/ index in a list. How amazing is that? This article discusses nested lists and operations on them with examples for a clear view of the concepts.

Syntax

`Outer_list = [a, b, c, [nested_list], d, e]`

Example program:

``````list1 = [1, 2, 3, 6]
list2 = [4, 5]
list1. insert (3, list2)
print ("Nested list: ", list1)
``````

Output:

`Nested list:  [1, 2, 3, [4, 5], 6]`

Understanding:

Using the insert () function, we inserted list2 into list1 at the 3rd index. list2 generally contains 2 elements, but the whole list occupies only the 3rd index in list1.

## Accessing the nested list

In the above example, to access the elements of list2, we need to first go to the index of the nested list, then to the index we want inside the nested list:

Syntax:

`Outer_list [index of nested list in outer list] [index of element in nested list]`

Example:

``````list1 = [1, 2, 3, [4, 5], 6]
print (list1)
print (list1  )
print (list1  )``````

Output:

``````[1, 2, 3, [4, 5], 6]
4
5``````

Understanding:

[4, 5] is in the 3rd index of the loop, and we accessed the two elements inside the nested list.

## Matrix representation: Lists representation of matrix 3 * 3:

### Creating a matrix: (using nested for loop)

``````list1 = []
for i in range (3):
list1. append ([])
for j in range (3):
list1 [i]. append (j)
for i in list1:
print (i)``````

Output:

``````[0, 1, 2]
[0, 1, 2]
[0, 1, 2]``````

Understanding:

First, we need to create an empty list and append it with nothing till number of rows we need to the matrix. Using another nested for loop, we append the values into the columns of the matrix.

i = 0 -> list1 = [__] -> j = 0 -> list1 = []

j = 1 -> list1 = [[0, 1]]

j = 2 -> list1 = [[0, 1, 2]]

i = 1 -> list1 = [__, __] -> j = 0 -> list1 = [[0, 1, 2], ]

j = 1 -> list1 = [[0, 1, 2], [0, 1]]

j = 2 -> list1 = [[0, 1, 2], [0, 1, 2]]

i = 2 -> list1 = [__, __, __] -> j = 0 -> list1 = [[0, 1, 2], [0, 1, 2], ]

j = 1 -> list1 = [[0, 1, 2], [0, 1, 2], [0, 1]]

j = 2 -> list1 = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]

Using list comprehensions, we can achieve the same output but in less number of lines. We can use nested list comprehension to create nested lists.

Using list comprehension:

``````list1 = [[j for j in range (3)] for i in range (3)]
for i in list1:
print (i)``````

Output:

``````[0, 1, 2]
[0, 1, 2]
[0, 1, 2]``````

Understanding:

We used nested list comprehension to create a nested list. You can observe that we nested a list comprehension inside a list comprehension.

[j for j in range (3)] is nested in [for i in range (3)]

## Program to add two matrices

``````Mat1 = [[11, 12, 13],
[14, 15, 16],
[17, 18, 19]]
Mat2 = [[21, 22, 23],
[24, 25, 26],
[27, 28, 29]]
sum12 = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
for i in range (len (Mat1)):
for j in range (len (Mat1 )):
sum12 [i][j] = Mat1 [i][j] + Mat2 [i][j]
for i in sum12:
print (i)``````

Output:

``````[32, 34, 36]
[38, 40, 42]
[44, 46, 48]``````

Understanding:

We kept three lists inside a list representing the three rows, the first element in the three lists representing the first column, etc. We initialized the sum as a zero matrix to update it in the next part of the program. In the outer for loop, i iterates through the length of the matrix-1: 3, which means 0, 1, and 2-represents the rows. In the inner for loop, j iterates through the length of the 0th index of matrix 1-length of the first row-3- represent the number of columns.

• The outer loop iterates through the rows, and the inner loop iterates through each element in the row.
• In the above example,

First iteration:

i = 0: j = 0, 1, 2

sum12  = Mat1  + Mat2  -> 11 + 21 = 32

sum12  = Mat1  + Mat2  -> 12 + 22 = 34

sum12  = Mat1  + Mat2  -> 13 + 23 = 36

[11, 12, 13] + [21, 22, 23] = [32, 34, 36]

Second iteration:

i = 1: j = 0, 1, 2

sum12  = Mat1  + Mat2  -> 14 + 24 = 38

sum12  = Mat1  + Mat2  -> 15 + 25 = 40

sum12  = Mat1  + Mat2  -> 16 + 26 = 42

[14, 15, 16] + [24, 25, 26] = [38, 40, 42]

Third iteration:

i = 2: j = 0, 1, 2

sum12  = Mat1  + Mat2  -> 17 + 27 = 44

sum12  = Mat1  + Mat2  -> 18 + 28 = 46

sum12  = Mat1  + Mat2  -> 19 + 29 = 48

[17, 18, 19] + [27, 28, 29] = [44, 46, 48]

Nested lists and nested list comprehension might be confusing in some situations, but it is an important concept and becomes easy once you grasp it.