Also known as: array, NumPy array
The ndarray (n-dimensional array) is NumPy's core data structure: a fixed-size grid of elements, all of the same data type, stored in a contiguous block of memory. Unlike Python lists, which are arrays of pointers to arbitrary objects, ndarrays store raw values packed together, enabling hardware-level optimisations (SIMD instructions, cache-friendly access patterns, and vectorised operations written in C).
An ndarray has a shape (a tuple giving the size along each dimension), a dtype (the data type of elements, e.g., float64, int32, bool), and a stride (the number of bytes to step in each dimension). You create arrays with np.array(), np.zeros(), np.ones(), np.arange(), np.linspace(), or by reading data from files. Arrays support slicing, fancy indexing (with arrays of indices), boolean masking, and reshaping — all without copying data when possible.
The ndarray's combination of type homogeneity, contiguous memory, and C-level operations makes it the backbone of Python's numerical ecosystem. Operations on ndarrays are vectorised: a + b adds two arrays element-wise in C, not with a Python loop. This is the key to writing fast numerical Python code — avoid explicit loops and let NumPy handle iteration at the C level.
Discussed in:
- Chapter 16: Working with Data — NumPy: The Numerical Foundation