🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

Basic Slicing

AI & DATA SCIENCE // basic-slicing

Basic slicing extends Python's start:stop:step slice syntax to multiple dimensions at once, returning a view into the original array rather than a copy.

Syntax

arr[start:stop:step, start:stop:step]

Deep Dive Course

For a multi-dimensional array, you provide one slice, or index, per dimension, separated by commas, e.g. selecting rows 1-2 and every other column at once. Using a bare colon for a dimension selects everything along it, and omitting trailing dimensions entirely selects all of them implicitly. Crucially, basic slicing always returns a view sharing the same underlying memory as the original array, so modifying a slice's elements modifies the original array's data too — this is a deliberate performance optimization, but a frequent source of surprising bugs.

1Understanding Basic Slicing

For a multi-dimensional array, you provide one slice, or index, per dimension, separated by commas, e.g. selecting rows 1-2 and every other column at once. Using a bare colon for a dimension selects everything along it, and omitting trailing dimensions entirely selects all of them implicitly. Crucially, basic slicing always returns a view sharing the same underlying memory as the original array, so modifying a slice's elements modifies the original array's data too — this is a deliberate performance optimization, but a frequent source of surprising bugs.

💡

Basic slicing, using colons and integers, returns a view, but fancy indexing, using a list or array of indices, always returns a copy — the two look similar but behave very differently regarding whether you're modifying the original data.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
print(matrix[1:3, 1:3])
localhost:3000

2Practical Example

Here is a real-world application of Basic Slicing showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.arange(10)
slice_view = arr[2:5]
slice_view[0] = 99
print(arr)
localhost:3000

3Best Practices

Follow these guidelines when working with Basic Slicing:

1. Call .copy() explicitly on a slice's result when you need an independent array and don't want changes to propagate back to the original

2. Use a bare colon to select an entire dimension explicitly, for readability, rather than relying on omitting trailing dimensions

3. Remember basic slicing views share memory with the original array — be deliberate about whether that shared-mutation behavior is what you actually want

⚠️

Tip: Basic slicing, using colons and integers, returns a view, but fancy indexing, using a list or array of indices, always returns a copy — the two look similar but behave very differently regarding whether you're modifying the original data.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
print(matrix[1:3, 1:3])
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

matrix = np.arange(16).reshape(4, 4)
print(matrix[1:3, 1:3])
Example 02Advanced Example
import numpy as np

arr = np.arange(10)
slice_view = arr[2:5]
slice_view[0] = 99
print(arr)

Best Practices

  • Call .copy() explicitly on a slice's result when you need an independent array and don't want changes to propagate back to the original
  • Use a bare colon to select an entire dimension explicitly, for readability, rather than relying on omitting trailing dimensions
  • Remember basic slicing views share memory with the original array — be deliberate about whether that shared-mutation behavior is what you actually want

Interview Question

Why does modifying elements through a basic slice of a NumPy array also change the original array?

Hint: Think about whether slicing allocates new memory or just describes a view into existing memory.

Basic slicing doesn't copy any data — it constructs a new array object that describes a view into the same underlying memory buffer as the original array, using adjusted strides and offsets to determine which existing bytes to expose. Since the slice and the original array both point at the exact same memory, writing to an element through the slice writes to that same memory location the original array reads from, so the change is visible through both. This is a deliberate design choice for performance, avoiding unnecessary copies, unlike Python's own list slicing, which always makes a copy.

Exercises

MediumPractice using Basic Slicing in a real scenario.
View Solution
import numpy as np

matrix = np.arange(16).reshape(4, 4)
print(matrix[1:3, 1:3])

Frequently Asked Questions

Why does modifying elements through a basic slice of a NumPy array also change the original array?

Basic slicing doesn't copy any data — it constructs a new array object that describes a view into the same underlying memory buffer as the original array, using adjusted strides and offsets to determine which existing bytes to expose. Since the slice and the original array both point at the exact same memory, writing to an element through the slice writes to that same memory location the original array reads from, so the change is visible through both. This is a deliberate design choice for performance, avoiding unnecessary copies, unlike Python's own list slicing, which always makes a copy.

Related Functions

fancy-indexingboolean-indexingndarray-shape