Python 2D Lists and Matrices

Printing the rows and columns from a 2D list

 
      m=[[1,1,1],[2,2,2]]
      #printing the number of rows or elements in the list
      print(len(m))
      #printing the number of columns in each row
      print(len(m[0])) 
    
Output

Print the list in a matrix format.

 
      m=[[1,1,1],[2,2,2]]
for i in range(len(m)):
    for x in range(len(m[i])):
        print(m[i][x],end="")
    print("")
    
Output

Changing the 1s to zeros in the matrix

 
      m=[[1,1,1],[2,2,2]]
      for i in range(len(m)):
          for x in range(len(m[i])):
              if m[i][x]==1:
                  m[i][x]=0
      
      print(m) 

    
Output

Reading value for a matrix from the user

 
      k=[[0,0],[0,0]]
for i in range(rows):
    for x in range(cols):
        k[i][x]=int(input())

print(k)

    
Output

Reading rows and columns from the user and creating a matrix

 
      rows=int(input())
      cols=int(input())
      k=[]
      g=[0]
      for i in range(rows):
          g=[]
          for j in range(cols):
              g.append(0)
          k.append(g)
      
      print("Enter the matrix elements")
      for i in range(rows):
          for j in range(cols):
              k[i][j]=int(input())
      
      print(k)
       

    
Output

Addition of two matrices

 
      rows=int(input())
      cols=int(input())
      matrix1=[]
      matrix2=[]
      resultmatrix=[]
      g=[0]
      #initialize matrix1
      for i in range(rows):
          g=[]
          for j in range(cols):
              g.append(0)
          matrix1.append(g)
      
      #initialize matrix2
      for i in range(rows):
          g=[]
          for j in range(cols):
              g.append(0)
          matrix2.append(g)
      
      #initialize matrix2
      for i in range(rows):
          g=[]
          for j in range(cols):
              g.append(0)
          resultmatrix.append(g)
      
      print("Enter the matrix elements for matrix 1")
      for i in range(rows):
          for j in range(cols):
              matrix1[i][j]=int(input())
      
      print("Enter the matrix elements for matrix 2")
      for i in range(rows):
          for j in range(cols):
              matrix2[i][j]=int(input())
      
      #find the sum of the matrices
      for i in range(rows):
          for j in range(cols):
              resultmatrix[i][j]=matrix1[i][j]+matrix2[i][j]
      
      print(resultmatrix)
       

    
Output

Check if an input matrix is an identity matrix

 
      rows=int(input())
cols=int(input())
m1=[]
m2=[]
result=[]
g=[]
flag=0
for i in range(rows):
    g=[]
    for j in range(cols):
        g.append(0)
    m1.append(g)

for i in range(rows):
    g=[]
    for j in range(cols):
        g.append(0)
    result.append(g)

print("Enter the values for the matrix1")
for i in range(rows):
    for j in range(cols):
        m1[i][j]=int(input())

for i in range(rows):
    for j in range(cols):
        if i==j and m1[i][j]!=1:
            flag=1
            break

if flag==0:
    print("Its an identity matrix")
else:
    print("Its not an identity matrix")


    
Output

Find the transpose of an input matrix

 
      rows=int(input())
cols=int(input())
m1=[]
m2=[]
result=[]
g=[]
for i in range(rows):
    g=[]
    for j in range(cols):
        g.append(0)
    m1.append(g)

for i in range(rows):
    g=[]
    for j in range(cols):
        g.append(0)
    result.append(g)


print("Enter the values for the matrix")
for i in range(rows):
    for j in range(cols):
        m1[i][j]=int(input())

#find the transpose
for i in range(rows):
    for j in range(cols):
        result[i][j]=m1[j][i]

print(result)
    
Output