NumPy empty and empty_like

np.empty allocates storage without setting numeric entries to useful values. I was surprised that “empty” here does not mean the array has zero elements.

You can use the dimensions you choose yourself or take the shape and dtype from a prototype. Compare what np.empty and np.empty_like return.

What np.empty and np.empty_like return

NumPy’s empty functions allocate an array without setting its numeric entries to useful values. “Empty” here means uninitialized storage, not an array with zero elements.

Use np.empty(shape) when you choose the dimensions yourself. Use np.empty_like(prototype) when the new array should inherit a prototype’s shape and data type.

With np.empty, shape is required and dtype defaults to float64. Its order option controls row-major or column-major layout. np.empty_like inherits shape and dtype from its prototype, and its order option can override the memory layout.

FunctionShape and dtypeEntries
np.empty(shape)Set by arguments, dtype defaults to float64Uninitialized for numeric types
np.empty_like(prototype)Copied from prototype unless overriddenUninitialized for numeric types
np.zeros(shape) or np.zeros_like(prototype)Chosen or copiedEvery entry is zero

What you need before allocating an array

Import NumPy under the name np before running the examples. In a shape of (2, 3), the dimensions request two rows and three columns, and dtype selects the representation for each element.

import numpy as np

Step 1: Allocate a new array with np.empty

Pass the intended dimensions to np.empty, then assign every element before you inspect or calculate with the array. The slice assignment below initializes the complete allocation, so the printed values do not depend on whatever occupied that memory before allocation.

import numpy as np

values = np.empty((2, 3), dtype=np.int64)
values[:] = np.arange(6).reshape(2, 3)
print(values)
print(values.shape, values.dtype)
NumPy empty array after every element is assigned
The allocated two-by-three array is filled before its values are printed.

np.arange(6) supplies known integers for reshape(2, 3), and assigning through values[:] writes into the allocated array rather than replacing the variable with another object.

Step 2: Match a prototype with np.empty_like

Give np.empty_like an array or array-like prototype when its shape and dtype are the starting point. You can override dtype when the new values need a different representation, but the allocation still needs filling before a read.

prototype = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16)
result = np.empty_like(prototype)
result[:] = np.arange(prototype.size).reshape(prototype.shape)
print(result)
print(result.shape, result.dtype)

as_float = np.empty_like(prototype, dtype=np.float64)

The prototype supplies default shape and dtype, not values, and the final call changes dtype alone while keeping the same dimensions.

Check the array after assigning its values

The assignments made the printed values [[0, 1, 2], [3, 4, 5]] in both arrays. Their shapes were (2, 3), with dtypes int64 for np.empty and int16 for np.empty_like.

I filled both allocations before printing, so the output comes from my assignments rather than any values in the storage before initialization. I used an integer dtype for np.empty and inherited int16 from the prototype for np.empty_like, which makes the dtype choice visible without reading arbitrary contents.

AllocationPrinted shapePrinted dtype
np.empty, after assignment(2, 3)int64
np.empty_like, after assignment(2, 3)int16

When an empty array has no elements

A zero-element array is different from an uninitialized array. Check array.size == 0 when you need to know whether the element count is zero. An array returned by np.empty((2, 3)) has six elements even before they are initialized.

The shape determines the number of allocated entries. It does not decide whether those entries have been assigned meaningful values.

python3 -c 'import numpy as np; a = np.empty((0, 3)); print(a.size)'
NumPy zero-element array size output in a terminal
The zero dimension makes the array’s size 0.

Choose allocation from the values you need

Use np.empty or np.empty_like only when your next operation will assign every entry before anything reads it. If zero is the intended starting value, choose np.zeros or np.zeros_like instead.

next_values = np.zeros_like(prototype)
print(next_values)

NumPy empty questions

These answers separate allocation from initialization and from a zero-element shape.

Does np.empty create an array filled with zeros?

No. Numeric entries are uninitialized and may contain arbitrary values. Assign every element before reading the array.

How do I create a NumPy array with no elements?

Pass a shape with a zero dimension, such as np.empty((0, 3)). Check array.size == 0 to test whether the array contains no elements.

What does np.empty_like copy?

By default, it takes the prototype’s shape and dtype. It does not copy the prototype’s values, and the new numeric entries must be assigned before they are read.

Tanishka Dhondge
Tanishka Dhondge
Articles: 59