Table of Contents



Introduction

NumPy array slices return views rather than copies of the original array. In other words, the copy is a new array whereas the view is just a view of the original array. This is in contrast to Python list slicing where list slices are copies.

The copy is independent of the original array and any changes made to the copy will not affect the original array. Conversely, any changes made to the original array will not affect the copy. On the other hand, the view is not an independent copy and any changes made to the view will affect the original array and vice versa.

Object Subarray Difference (Copy vs View)
Python List Copy Changes to subarray will not modify array.
NumPy Array View Changes to subarray will modify array.

Python List Slices Return Copies

As mentioned, a Python list slice returns a copy of the original array. This is demonstrated in the following example.

Example

Create a 1-D random Python list.

1import random
2random.seed(10)
3a1 = random.sample(range(10),10)
4print(a1)
[9, 0, 6, 3, 4, 8, 1, 7, 2, 5]

In the above example, we created a 1-D random Python list using the random.sample() function. We now extract a subarray from the a1 Python list.

Example

Extract a subarray of the Python list.

1a1_slice = a1[1:4]
2print(a1_slice)
[0, 6, 3]

Finally, we modify the subarray.

Example

Modify the extracted subarray.

1a1_slice[0] = 99
2print(a1_slice)
3print(a1)
[99, 6, 3]
[9, 0, 6, 3, 4, 8, 1, 7, 2, 5]

As mentioned the a1_slice subarray is just a copy of the original list. The a1_slice subarray is modified but the original list a1 remains unchanged.

ADVERTISEMENT



NumPy Array Slices Return Views

We first create a 1-D NumPy array identical to the Python list created in the previous section.

Example

Create a 1-D NumPy array.

1a2 = np.array(a1)
2print(a2)
[9 0 6 3 4 8 1 7 2 5]

We now extract a subarray from the a2 NumPy array.

Example

Extract a subarray of the NumPy array.

1a2_slice = a2[1:4]
2print(a2_slice)
[0, 6, 3]

Finally, we modify the subarray.

Example

Modify the extracted subarray.

1a2_slice[0] = 99
2print(a2_slice)
3print(a2)
[99  6  3]
[ 9 99  6  3  4  8  1  7  2  5]

As mentioned the a2_slice subarray is a view of the original NumPy array a2. Both the a2_slice subarray and the original NumPy array a2 are modified!

At first sight, this feature of NumPy arrays might seem undesirable since it may lead to inadvertent changes to the original array. However, this default behavior is actually quite useful especially when we are working with large datasets where we can access and process segments of these datasets without the need to copy the underlying data buffer.

Creating Copies of NumPy Arrays

Despite the nice features of array views, NumPy also offers a method to explicitly copy the data within an array so that the subarray behaves as a copy rather than a view. This can be done with the copy() method:

Example

Extract a copy of array a2 using the copy() method.

1a2 = np.array(a1)
2print(a2)
3a2_subcopy = a2[1:4].copy()
4print(a2_subcopy)
[9 0 6 3 4 8 1 7 2 5]
[0 6 3]

We now modify the subarray.

Example

Modify the extracted subarray.

1a2_subcopy[0] = 99
2print(a2_subcopy)
3print(a2)
[99  6  3]
[9 0 6 3 4 8 1 7 2 5]

Since the a2_subcopy subarray is now a copy of the original NumPy array a2, the original array a2 remains unchanged even though the a2_subcopy subarray is modified.