Copied variable changes the original?

The line

aux=matriz;

Does not make a copy of matriz, it merely creates a new reference to matriz named aux. You probably want

aux=matriz[:]

Which will make a copy, assuming matriz is a simple data structure. If it is more complex, you should probably use copy.deepcopy

aux = copy.deepcopy(matriz)

As an aside, you don’t need semi-colons after each statement, python doesn’t use them as EOL markers.

Leave a Comment