Welcome to another chapter of our Python Tutorial series, Learn Python with me. In this chapter, we will explore the concept of Nested Lists in Python.
Link to all chapters related to Python: Learn Python
What are Nested Lists?
A list that is present in another list is called a Nested List. A list can contain any number of elements. You know that probably from the previous chapters. Using the concept of Nested Lists, you can add another list as an element within a list. It might not sound obvious to you now. That’s why let’s now look at a program.
#Python program to understand the concept of
#Nested Lists
list1=[55,66,77]
list2=[11,22,33,44,list1]
print("The entire list is: ",list2)
#let us print elements in the nested list using for loop
print("Let us print the elements from Nested Loop using for loop first: ")
i=1
for item in list2[4]:
print("Element {} in Nested list is {}".format(i,item))
i+=1
#another way to print the Nested list
print("Printing the elements in Nested List using their index positions")
print("The first element: ",list2[4][0])
print("The second element: ",list2[4][1])
print("The third element: ",list2[4][2])
Output
The entire list is: [11, 22, 33, 44, [55, 66, 77]]
Let us print the elements from Nested Loop using for loop first:
Element 1 in Nested list is 55
Element 2 in Nested list is 66
Element 3 in Nested list is 77
Printing the elements in Nested List using their index positions
The first element: 55
The second element: 66
The third element: 77
We declared two different lists in the program mentioned above, list1 and list2. We then added the entire list1 as the fourth element in list2. You can see that when we tried printing list2 in the output, the nested list was included at the index position four.
We tried two ways to display the Nested List elements in the example above. We used a for loop to display the elements and later showed them using their corresponding index positions. Since the Nested List was placed at index position 4 in list2, we used list2[4][0] to display the first element of the Nested List and so on.
Can we use Nested Lists to create Matrices?
A matrix is a depiction of a group of rows and columns. If there are a ‘p’ number of rows and a ‘q’ number of columns, it is called a ‘p x q’ matrix. In Python, we create matrices using the packages from numpy. However, you can develop matrices using Nested Lists as well. Let us find out how.
#python program on matrices addition
#using Nested lists
#Let us declare two matrices by the order 3 X 4
matrix1=[[1,2,3,0],[4,5,6,0],[7,8,9,0]]
matrix2=[[1,2,3,4],[1,0,1,0],[2,-1,2,-1]]
#declare another 3 X 4 matrix with only zeroes in it
matrix3=[4*[0] for i in range(3)]
#add the elements of matrix1 and matrix2 and store
#the result in matrix3
for i in range(3):
for j in range(4):
matrix3[i][j]=matrix1[i][j]+matrix2[i][j]
#display the result in matrix3
for a in range(3):
for b in range(4):
print("%d"%matrix3[a][b],end=" ")
print()
Output
The Nested List as it is:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Let us display row by row
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Let us display each column in row 0
1 2 3
Let us display each column in row 1
4 5 6
Let us display each column in row 2
7 8 9
Let us display all elements using for loop in first way:
1 2 3
4 5 6
7 8 9
Let us display all elements using for loop in second way:
1 2 3
4 5 6
7 8 9
In the program mentioned above, we performed the sequence mentioned below.
- Declare a Nested List
- Displayed the Nested List row by row, more like a matrix
- Displayed the columns in each row separately
- Displayed all the elements using a for loop and in the traditional way
- Displayed all the elements using for loop and the index positions
To better understand the program mentioned above, we’d request you to practice the same line by line on your Python IDE.
Let us look at one more program in which we can perform the addition of matrices using Nested Lists.
#python program on matrices addition
#using Nested lists
#Let us declare two matrices by the order 3 X 4
matrix1=[[1,2,3,0],[4,5,6,0],[7,8,9,0]]
matrix2=[[1,2,3,4],[1,0,1,0],[2,-1,2,-1]]
#declare another 3 X 4 matrix with only zeroes in it
matrix3=[4*[0] for i in range(3)]
#add the elements of matrix1 and matrix2 and store
#the result in matrix3
for i in range(3):
for j in range(4):
matrix3[i][j]=matrix1[i][j]+matrix2[i][j]
#display the result in matrix3
for a in range(3):
for b in range(4):
print("%d"%matrix3[a][b],end=" ")
print()
Output
2 4 6 4
5 5 7 0
9 7 11 -1
In the program above, we declared two matrices of the order 3X4, performed addition and then stored the result into matrix3. The matrix3 with the result was displayed as the output.