.pop() affects two lists at the same time

  list, python

I was trying to make programm that finds determinant of any given matrix and in the next bit of programm I was trying to find all minors

def deff(m,y=[]):
  #Here must be other parts of the programm
  #But I don't think they are affecting this bit
  x=0
  for i in range(len(m)): #m is the matrix(2d list)
    y=m[:][:]
    k=y[0][i] 
    y.pop(0) #removing first row
    for j in range(len(y)):
    y[j].pop(i) #removing column
    x+=k*deff(y)*(-1**(1+i)) 
  return x

And here comes the problem. When the programm does it’s first pop (y.pop(0)) it removes list only from y, but when it does it’s second pop (y[j].pop(i)) it removes numbers from m too. And I can’t find the solution. I tried other ways of assignment (as you can see in the programm, I was not using [:] but it didn’t help), I tried to crating two seperate massives, other ways to delete numbers from list like delete and using other’s ways to delete column, but no matter what I do it always deletes numbers from m

Source: Python Questions

LEAVE A COMMENT