Though Numpy library or Pandas DataFrame is preferred for matrix handling but still we can use multidimensional lists in python for many requirements.
Creating two dimensional list
my_list=[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0],
[3, 9, 1, 1, 5], [0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
Displaying all elements as matrix
my_list=[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0], [3, 9, 1, 1, 5],
[0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
for i in my_list:
print(i)
Output
[1, 0, 0, 4, 6]
[3, 9, 8, 0, 0]
[3, 9, 1, 1, 5]
[0, 9, 3, 0, 8]
[8, 6, 1, 9, 7]
Displaying elements
print(my_list[3][4]) # 8
Displaying a row of data , 0th row is first row , so this will display the 3rd row from top.
print(my_list[2]) # [3, 9, 1, 1, 5]
Displaying column ( 2nd column , first column is 0th column )
for x in my_list:
# print(x[3])
Or we can also write like this.
[x[3] for x in my_list]
Output
[4, 0, 1, 0, 9]
Display all elements of two dimensional list.
Here len() returns the number of elements in each row.
l1=[['abc','def','ghi','jkl'],
['mno','pkr','frt','qwr'],
['asd','air','abc','zpq'],
['zae','vbg','qir','zab']]
for my_row in l1: # Each row of data from the list
for data in range(len(my_row)): # Index position of Each element in the row
print(my_row[data],end=' ') # Print each element without line break
print('') # One line break after each row of data
Output
abc def ghi jkl
mno pkr frt qwr
asd air abc zpq
zae vbg qir zab
Deleting rows and columns
Delete 2nd row
my_list=[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0], [3, 9, 1, 1, 5], [0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
my_list.pop(2) # delete 2nd row , First row is 0th row
print(my_list)
Output
[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0], [0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
Delete column
my_list=[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0], [3, 9, 1, 1, 5], [0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
[x.pop(3) for x in my_list] # delete 3rd column,
print(my_list)
Output
[[1, 0, 0, 6], [3, 9, 8, 0], [3, 9, 1, 5], [0, 9, 3, 8], [8, 6, 1, 7]]
Or we can delete column like this
for x in my_list:
x.pop(3)
print(my_list)
Creating a two dimensional List by using random numbers
from random import randrange
n=5 # dimension of the matrix
my_list=[] # declare a list
for i in range(0,n):
l1=[]
for j in range(0,n):
x=randrange(10)
l1.append(x) # adding element to the list
my_list.append(l1)
print(my_list)
Output
[[5, 1, 7, 6, 6], [9, 0, 1, 1, 2], [9, 2, 2, 7, 4], [6, 2, 6, 9, 6], [7, 1, 6, 4, 5]]
Transpose of a matrix
We used zip here
my_list=[[1,2], [3,4], [5,6]]
for row in my_list:
print(row)
print("Transpose the matrix")
#list(map(list, zip(*my_list)))
my_list_T=map(list, zip(*my_list))
list(my_list_T)
Output
[1, 2]
[3, 4]
[5, 6]
Transpose the matrix
[[1, 3, 5], [2, 4, 6]]
Sum of diagonal numbers of a square matrix
my_list=[[1, 0, 0, 4, 6], [3, 9, 8, 0, 0], [3, 9, 1, 1, 5], [0, 9, 3, 0, 8], [8, 6, 1, 9, 7]]
for i in my_list:
print(i)
Output
[1, 0, 0, 4, 6]
[3, 9, 8, 0, 0]
[3, 9, 1, 1, 5]
[0, 9, 3, 0, 8]
[8, 6, 1, 9, 7]
« List Matrix Multiplication without using built-in functions
« Python
← Subscribe to our YouTube Channel here