Skip to main content
Albero Academy

Views vs Copies

Know when an operation shares memory with the original — the source of many surprises.

Indexing & Slicing8 min

A subtle but crucial NumPy concept: many operations return a view that shares memory with the original array, while others return an independent copy. Knowing which prevents both aliasing bugs and wasted memory.

Slices are views

a slice shares memory
a = np.array([1, 2, 3, 4])
s = a[1:3] # a VIEW
s[0] = 99
a # [ 1 99 3 4] — a changed too!

Making a copy

.copy() for independence
safe = a[1:3].copy() # independent
safe[0] = 0
a # unchanged

Which is which?

Returns a viewReturns a copy
Basic slicing (a[1:3])Fancy indexing (a[[0,2]])
reshape (usually)Boolean-mask indexing
.T (transpose).copy(), astype()

Key takeaways

  1. 01A view shares memory with the original; writing to it changes the source.
  2. 02Basic slices, reshape and .T are views; fancy/boolean indexing and .copy()/.astype() are copies.
  3. 03Use .copy() when you need an independent array.
  4. 04arr.base reveals whether an array is a view (points at the source) or a copy (None).

Finished this chapter?

Mark it complete to track your progress through NumPy.