Consider the two following codes:
import numpy as np
mainlist = [np.array([0,0,0, 1]), np.array([0,0,0,1])]
for i in range(len(mainlist)):
mainlist[i] = mainlist[i][0:2]
print(mainlist) # [array([0, 0]), array([0, 0])] => OK!
and:
import numpy as np
mainlist = [np.array([0,0,0, 1]), np.array([0,0,0,1])]
for element in mainlist:
element = element[0:2]
print(mainlist) # [array([0, 0, 0, 1]), array([0, 0, 0, 1])] => WTF?
I was wondering why, in the second case, the arrays remain unchanged. It does not even throw an error about mutability problems. Could you explain exactly what is going on regarding the behavior of the second code? What would be the right way of doing it instead?