numpy icon

Indexing & Slicing

Expert Answer & Key Takeaways

A complete guide to understanding and implementing Indexing & Slicing.

Indexing & Slicing in NumPy (2026)

NumPy offers extremely powerful ways to access and modify data in n-dimensional arrays. Understanding the difference between Views and Copies is the hallmark of a Senior Numerical Engineer.

1. The Proof Code (Views vs. Copies)

Demonstrating how basic slicing shares memory with the original array, while fancy indexing creates a new object.
import numpy as np import numpy.typing as npt # 1. The View (Basic Slicing) original: npt.NDArray[np.int64] = np.array([1, 2, 3, 4, 5]) view_slice = original[:3] view_slice[0] = 99 print(f"Original after view mod: {original[0]}") # Output: 99 (Shared memory) # 2. The Copy (Fancy Indexing) fancy_copy = original[[0, 1, 2]] fancy_copy[0] = -1 print(f"Original after copy mod: {original[0]}") # Output: 99 (Independent memory) # 3. Boolean Masking (Filtering) mask = original > 50 filtered = original[mask] print(f"Filtered elements: {filtered}")

2. Execution Breakdown

  1. Basic Slicing: Returns a View. It only changes the metadata (offset, shape, strides) of the array object without duplicating the raw memory buffer.
  2. Fancy Indexing: Occurs when you pass a list or another array as an index. This triggers a memory allocation and returns a Copy.
  3. Boolean Indexing: Internally uses the __getitem__ method with a boolean mask. This is highly optimized and allows for massive data filtering without explicit loops.

3. Detailed Theory

Memory Strides

When you slice an array, NumPy doesn't move data. It simply updates the strides (how many bytes to skip to reach the next element in an axis). This is why slicing is O(1)O(1) in time and memory.

Integer Array Indexing

Also known as 'Fancy Indexing', it allows you to select elements in any order. Because the resulting layout might not be representable by strides alone, NumPy is forced to create a new contiguous array.

Multi-dimensional Slicing

Unlike Python lists (list[0][1]), NumPy uses a single tuple-based index (arr[0, 1]). This allows the underlying C-code to calculate the memory offset in a single step, rather than traversing multiple object references.

4. Senior Secret

To detect if an array is a view or a copy, check the .base attribute. If arr.base is not None, it is a view of another array. If you need to force a copy to prevent unintended side effects, always use arr.copy() explicitly.

5. Interview Corner

Integrated Interview Questions for SEO & FAQ Schema.

Top Interview Questions

?Interview Question

Q:What happens to the original array if you modify a slice obtained via basic indexing?
A:
The original array is modified because basic slicing returns a View (a shallow reference to the same memory buffer).

?Interview Question

Q:How can you check if an array owns its own memory or is a view of another array?
A:
Check the .base attribute. If it returns an object, the array is a view. If it returns None, the array owns its memory.
numpy icon

Course4All Data Team

Verified Expert

Numerical Computing Experts

Our NumPy curriculum is crafted by scientific computing specialists to ensure deep understanding of vectorized operations and memory-efficient numerical analysis.

Pattern: 2026 Ready
Updated: Weekly