Now I know that it is not safe to modify the list during an iterative looping. However, suppose I have a list of strings, and I want to strip the strings themselves. Does re
The answer given by Jemshit Iskenderov and Ignacio Vazquez-Abrams is really good. It can be further illustrated with this example: imagine that
a) A list with two vectors is given to you;
b) you would like to traverse the list and reverse the order of each one of the arrays
Let's say you have
v = np.array([1, 2,3,4])
b = np.array([3,4,6])
for i in [v, b]:
i = i[::-1] # this command does not reverse the string
print([v,b])
You will get
[array([1, 2, 3, 4]), array([3, 4, 6])]
On the other hand, if you do
v = np.array([1, 2,3,4])
b = np.array([3,4,6])
for i in [v, b]:
i[:] = i[::-1] # this command reverses the string
print([v,b])
The result is
[array([4, 3, 2, 1]), array([6, 4, 3])]