O'Reilly logo
  • Mrinali Gupta thinks this is interesting:

An important first distinction from Python’s built-in lists is that array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array.

From

Cover of Python for Data Analysis, 2nd Edition

Note

To give an example of this, I first create a slice of arr:

In [66]: arr_slice = arr[5:8]

In [67]: arr_slice Out[67]: array([12, 12, 12]) Now, when I change values in arr_slice, the mutations are reflected in the original array arr:

In [68]: arr_slice[1] = 12345

In [69]: arr Out[69]: array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8,
9]) The “bare” slice [:] will assign to all values in an array:

In [70]: arr_slice[:] = 64

In [71]: arr Out[71]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9]) If you are new to NumPy, you might be surprised by this, especially if you have used other array programming languages that copy data more eagerly. As NumPy has been designed to ...