Skip to content
tezvyn:
Menu

NumPy ndarray: Fast, Typed, Multidimensional Grids

Source: numpy.orgEasyHow cards are made

A NumPy ndarray is a fast, memory-efficient grid for numbers of a single type. It's the backbone for scientific computing, used for image data to ML model weights. The main footgun: slicing often creates a view, not a copy, so edits can alter the original.

Why it exists

Python lists are flexible but slow for numerical work because they store pointers to objects of varying types. For science and data analysis, we need a dense, contiguous block of memory holding a single data type to perform fast, vectorized math. The ndarray was created to be this high-performance data container.

The mental model

Think of an ndarray not as a list of items, but as a single block of memory with a map laid on top. This map, defined by the array's shape, strides, and data type (dtype), tells NumPy how to interpret the raw bytes as a multidimensional grid. This is why operations are so fast—they often happen in optimized, pre-compiled C code.

How it works

An ndarray consists of a pointer to a contiguous block of data, plus metadata. Key attributes are: shape, a tuple defining the size of each dimension (e.g., (2, 3) for a 2x3 matrix); dtype, which specifies the data type of every element (e.g., int32, float64); and strides, a tuple telling NumPy how many bytes to jump in memory to get to the next element along each dimension. This stride mechanism allows for efficient 'views' without copying data.

When to use it

Use an ndarray whenever you have numerical data that needs to be processed efficiently. This includes mathematical and statistical operations, machine learning, image processing, signal processing, and any task involving large, homogeneous datasets. Its performance vastly outstrips standard Python lists for these tasks.

When not to use it

Avoid ndarrays for collections of heterogeneous data, like a mix of strings, integers, and objects; a Python list is better. Also, if you need a container that frequently changes size by appending or removing elements, the fixed-size nature of ndarrays is inefficient, as it requires creating a new array and copying data each time.

One canonical example

Slicing an array can create a 'view,' not a copy. If you have an array x = np.array([10, 20, 30]) and create a slice y = x[0:2], y is a view. If you then change an element in the slice, like y[0] = 99, the original array x is also changed and becomes [99, 20, 30]. To get a true copy, you must explicitly use y = x[0:2].copy(). This behavior is a feature for performance but a common source of bugs.

Interview question

Which statement accurately describes a common behavior when slicing a NumPy ndarray?

  • a.Slicing an ndarray always creates a new, distinct copy of the data, preventing accidental modification of the original.
  • b.Modifying elements in the sliced portion will also alter the original array if the slice is a view.Correct
  • c.Slicing an ndarray automatically converts its elements to Python's native list type for easier manipulation.
  • d.Slicing an ndarray is generally slower than iterating through its elements using a Python for loop.
Why?

The card explicitly states that 'slicing often creates a view, not a copy, so edits can alter the original.' This behavior is a feature for performance but a common source of bugs. Option A is a common misconception, as slicing does not always create a copy; it often creates a view.

Just read this? Test yourself on what you have been reading.

Read the original → numpy.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles