Nested List
A list within list is called nested list
Representing a list as an element inside list is called nested list
Nested list can be represented as matrix
Syntax:
[[ele,ele,..],[ele,ele,..],..]
# representing details of 3 students and each student is having
# rollno,name,course
students=[[1,'naresh','python'],[2,'suresh','java'],[3,'kishore','c']]
for i in range(len(students)): # 0 1 2
for j in range(len(students[i])): # 0 1 2
print(students[i][j],end=' ')
print()
for stud in students:
for value in stud:
print(value,end=' ')
print()
Output:
1 naresh python
2 suresh java
3 kishore c
1 naresh python
2 suresh java
3 kishore c
Example:
# write a program to read 3x3 matrix and display
matrix=[]
for i in range(3):
row=[]
for j in range(3):
ele=int(input("enter any element"))
row.append(ele)
matrix.append(row)
for row in matrix:
for col in row:
print(col,end=' ')
print()
print(matrix)
Output:
enter any element1
enter any element2
enter any element3
enter any element4
enter any element5
enter any element6
enter any element7
enter any element8
enter any element9
123
456
789
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Example:
# write a program to read m students n subject marks and display
m=int(input("How many students?"))
n=int(input("How many subjects?"))
students=[]
for i in range(m):
stud=[]
for j in range(n):
s=int(input("Marks:"))
stud.append(s)
students.append(stud)
print(students)
for stud in students:
for s in stud:
print(s,end=' ')
print()
for stud in students:
print(f'{stud}\t{sum(stud)}\t{sum(stud)/n:.2f}')
Output:
How many students?2
How many subjects?3
Marks:60
Marks:70
Marks:80
Marks:80
Marks:99
Marks:89
[[60, 70, 80], [80, 99, 89]]
60 70 80
80 99 89
[60, 70, 80] 21070.00
[80, 99, 89] 26889.33
>>>
Displays
Displays for lists, sets and dictionaries
For constructing a list, a set or a dictionary Python provides special syntax
called “displays”, each of them in two flavors:
either the container contents are listed explicitly, or
they are computed via a set of looping and filtering instructions, called
a comprehension.
Syntax of list comprehension
Syntax-1: [expression for variable in iterable]
Syntax-2: [expression for variable in iterable if test]
Syntax-1: [expression for variable in iterable]
Example:
# create a list with sqr all the numbers from 1 to 50
list1=[]
for num in range(1,51):
list1.append(num**2)
print(list1)
list2=[num**2 for num in range(1,51)]
print(list2)
Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324,
361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024,
1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936,
2025, 2116, 2209, 2304, 2401, 2500]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324,
361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024,
1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936,
2025, 2116, 2209, 2304, 2401, 2500]
Example:
# input n values in list
# without comprehension
list1=[]
n=int(input("enter the value n"))
for i in range(n):
ele=int(input("enter element"))
list1.append(ele)
print(list1)
list2=[int(input("enter element")) for i in range(n)]
print(list2)
Output:
enter the value n5
enter element1
enter element2
enter element3
enter element4
enter element5
[1, 2, 3, 4, 5]
enter element1
enter element2
enter element3
enter element4
enter element5
[1, 2, 3, 4, 5]
>>>
Example:
# write a program to 3x3 matrix and display
# without comprehension
matrix=[]
for i in range(3):
row=[]
for j in range(3):
row.append(int(input("enter value")))
matrix.append(row)
print(matrix)
matrix1=[[int(input("enter value")) for j in range(3)] for i in range(3)]
print(matrix1)
Output:
enter value1
enter value2
enter value3
enter value4
enter value5
enter value6
enter value7
enter value8
enter value9
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
enter value1
enter value2
enter value3
enter value4
enter value5
enter value6
enter value7
enter value8
enter value9
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>
# adding two matrices
print("First Matrix")
matrix1=[[int(input()) for j in range(2)] for i in range(2)]
print("Second Matrix")
matrix2=[[int(input()) for j in range(2)] for i in range(2)]
matrix3=[[matrix1[i][j]+matrix2[i][j] for j in range(2)] for i in range(2)]
print(matrix1)
print(matrix2)
print(matrix3)
Output:
First Matrix
1
2
3
4
Second Matrix
5
6
7
8
[[1, 2], [3, 4]]
[[5, 6], [7, 8]]
[[6, 8], [10, 12]]
>>>