Adding Axes: newaxis & expand_dims
np.newaxis (and its function form np.expand_dims) inserts a new length-1 dimension into an array, reshaping a flat vector into a row or a column so that broadcasting can combine it with other arrays.
Learn Adding Axes: newaxis & expand_dims in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll add axes with newaxis and expand_dims, remove size-1 axes with squeeze, and use a column-versus-row vector to build a grid of values entirely through broadcasting.
A 1D array of shape (3,) is neither a row nor a column — it is just a flat sequence. Indexing with np.newaxis (which is literally None ) inserts a length-1 axis to give it a 2D shape. Put the new axis last for a column (3, 1) , or first for a row (1, 3) .
np.expand_dims(arr, axis) does the same job as newaxis but as a function call, which is handy when the axis number is computed. Its inverse is np.squeeze , which deletes every length-1 axis (or just the one you name), collapsing a shape like (1, 3, 1) back down to (3,) .
The main reason to add axes is to unlock broadcasting . Make one vector a column (n, 1) and another a row (1, m) ; combining them stretches each across the other to produce an (n, m) grid. This is how you build an addition table or an outer product without any loops — a lighter-weight alternative to meshgrid.
Turn v into a column vector of shape (4, 1) by filling in the indexing token.
Answer: newaxis (so v[:, np.newaxis] ). You could also write v[:, None] .
❌ Two 1D arrays will not broadcast into a grid
Adding two shape-(3,) arrays just adds them element-wise, not as a table:
✅ Fix: add axes so one is a column and one is a row:
A bare np.squeeze(arr) drops every size-1 axis at once.
✅ Fix: name the axis, e.g. np.squeeze(arr, axis=0) , to remove only that one.
v[np.newaxis, :] makes a row, v[:, np.newaxis] makes a column — easy to swap.
✅ Fix: print .shape after adding the axis to confirm it is (1, n) or (n, 1) .
Use newaxis to compute the difference between every pair of values in a vector — a common step in distance calculations.
Lesson complete — you can reshape with new axes!
You can insert a length-1 dimension with np.newaxis or np.expand_dims , remove one with np.squeeze , and use a column-versus-row pairing to broadcast a full grid.
🚀 Up next: Transposing & Swapping Axes — reorder the dimensions of an array with .T , transpose , and swapaxes .
Practice quiz
What is np.newaxis literally equal to?
- None
- 0
- An empty array
- 1
Answer: None. np.newaxis is just an alias for Python's None, so v[:, None] and v[:, np.newaxis] are identical.
What shape does v[:, np.newaxis] give a shape-(3,) array?
- (1, 3)
- (3, 1)
- (3,)
- (3, 3)
Answer: (3, 1). Putting the new axis last makes a column vector of shape (3, 1).
What shape does v[np.newaxis, :] give a shape-(3,) array?
- (3, 1)
- (3, 3)
- (1, 3)
- (3,)
Answer: (1, 3). Putting the new axis first makes a row vector of shape (1, 3).
Which function inserts a size-1 axis using a number argument?
- np.squeeze
- np.reshape
- np.ravel
- np.expand_dims
Answer: np.expand_dims. np.expand_dims(arr, axis=1) adds a length-1 axis at the given position, the function form of newaxis.
What does np.squeeze do by default?
- Removes every length-1 axis
- Adds a new axis
- Flattens to 1D always
- Transposes the array
Answer: Removes every length-1 axis. Bare np.squeeze drops all size-1 dimensions; name an axis to remove just one.
What shape does np.expand_dims(v, axis=0) produce from shape (3,)?
- (3, 1)
- (1, 3)
- (3,)
- (3, 3)
Answer: (1, 3). axis=0 inserts the new length-1 axis first, giving (1, 3).
To broadcast a column against a row into an (n, m) grid, you need...
- Two 1D arrays of the same length
- One (n, 1) and one (1, m) array
- Two scalars
- A single (n, m) array
Answer: One (n, 1) and one (1, m) array. A column (n, 1) and a row (1, m) broadcast into an (n, m) grid.
Does adding an axis with newaxis move or copy the underlying data?
- It copies all the data
- It sorts the data
- No, only the shape changes
- It deletes elements
Answer: No, only the shape changes. newaxis only changes the shape metadata; the data is never moved.
Which removes only the first axis from shape (1, 3, 1)?
- np.squeeze(a, axis=0)
- np.squeeze(a)
- np.expand_dims(a, 0)
- a.ravel()
Answer: np.squeeze(a, axis=0). Naming axis=0 removes only that size-1 axis, leaving (3, 1).
What does a[:, None] + b[None, :] compute for 1D a and b?
- An element-wise sum
- A scalar
- A broadcast grid of pairwise sums
- An error
Answer: A broadcast grid of pairwise sums. Pairing a column with a row broadcasts into a full 2D grid of pairwise sums.