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