input: Write a function that creates a Matrix class in Python to perform operations such as addition, subtraction, multiplication, and transpose. The class should take the number of rows (m), number of columns (n), and a list of elements (lst) to initialize the matrix. Provide implementations for the methods to add, subtract, multiply, and transpose matrices. If the operations are not possible due to size mismatches, handle the errors or exceptions appropriately.
  
  class Matrix: 
      def __init__(self, m, n, lst): 
          self.m = m             
          self.n = n             
          self.mat = [] 
    
          c = 0
          for i in range(self.m): 
              a =[] 
              for j in range(self.n): 
                      a.append(lst[c]) 
                      c += 1
              self.mat.append(a) 
    
      def __str__(self): 
          output = ""
          for i in self.mat: 
              for j in i: 
                  output += str(j) + " "
              output += '\n'
          return output 
    
      def add(self, mat2): 
          for i in range(self.m): 
              for j in range(self.n): 
                  self.mat[i][j] += mat2.mat[i][j] 
    
      def sub(self, mat2): 
          for i in range(self.m): 
              for j in range(self.n): 
                  self.mat[i][j] -= mat2.mat[i][j]
      
      def mul(self, mat2): 
          result = [] 
          for i in range(self.m): 
              a =[] 
              for j in range(mat2.n): 
                  s = 0
                  for k in range(self.n): 
                      s += self.mat[i][k] * mat2.mat[k][j] 
                  a.append(s) 
              result.append(a) 
          self.mat = result 
          self.m = len(result) 
          self.n = len(result[0]) 
    
      def transpose(self):
          result = [[self.mat[j][i] for j in range(len(self.mat))] for i in range(len(self.mat[0]))]
          self.mat = result 
          self.m = len(result) 
          self.n = len(result[0])
  
  assert str(Matrix(2, 2, [1, 2, 3, 4])) == "1 2 \n3 4 \n"
  assert str(Matrix(2, 2, [1, 2, 3, 4]).transpose()) == "1 3 \n2 4 \n"
  mat1 = Matrix(2, 2, [1, 2, 3, 4])
  mat1.add(Matrix(2, 2, [5, 6, 7, 8]))
  assert str(mat1) == "6 8 \n10 12 \n"
  mat1 = Matrix(2, 2, [1, 2, 3, 4])
  mat1.sub(Matrix(2, 2, [5, 6, 7, 8]))
  assert str(mat1) == "-4 -4 \n-4 -4 \n"
  mat1 = Matrix(2, 2, [1, 2, 3, 4])
  mat1.mul(Matrix(2, 2, [5, 6, 7, 8]))
  assert str(mat1) == "19 22 \n43 50 \n"
output: def __init__(self, m, n, lst): 
      self.m = m             
      self.n = n             
      self.mat = [] 
  
      c = 0
      for i in range(self.m): 
          a =[] 
          for j in range(self.n): 
                  a.append(lst[c]) 
                  c += 1
          self.mat.append(a) 
  
  def __str__(self): 
      output = ""
      for i in self.mat: 
          for j in i: 
              output += str(j) + " "
          output += '\n'
      return output 
  
  def add(self, mat2): 
      if self.m != mat2.m or self.n != mat2.n:
          raise ValueError("Matrices must have the same dimensions for addition.")
      for i in range(self.m): 
          for j in range(self.n): 
              self.mat[i][j] += mat2.mat[i][j] 
  
  def sub(self, mat2): 
      if self.m != mat2.m or self.n != mat2.n:
          raise ValueError("Matrices must have the same dimensions for subtraction.")
      for i in range(self.m): 
          for j in range(self.n): 
              self.mat[i][j] -= mat2.mat[i][j]
  
  def mul(self, mat2): 
      if self.n != mat2.m:
          raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.")
      result = [] 
      for i in range(self.m): 
          a =[] 
          for j in range(mat2.n): 
              s = 0
              for k in range(self.n): 
                  s += self.mat[i][k] * mat2.mat[k][j] 
              a.append(s) 
          result.append(a) 
      self.mat = result 
      self.m = len(result) 
      self.n = len(result[0]) 
  
  def transpose(self):
      result = [[self.mat[j][i] for j in range(len(self.mat))] for i in range(len(self.mat[0]))]
      self.mat = result 
      self.m = len(result) 
      self.n = len(result[0])