np.empty(shape, dtype=...) creates a NumPy array with the requested shape and data type, but it does not initialize ordinary element values. A zero-length array is different: its shape includes a dimension of length zero, so it contains no elements to initialize. Use np.zeros when values must begin at zero, and assign every element of a nonzero np.empty array before reading it.
What does np.empty() do?
NumPy documents numpy.empty as returning a new array of a given shape and type “without initializing entries.” In other words, the function reserves an array with the requested metadata, but you must not assume ordinary entries have any particular value. Its contents are arbitrary until you write to them. See the NumPy empty reference.
For correctness and reproducibility, write every element that your program will read. If you need an array whose entries start at zero, use np.zeros instead.
What is a zero-length NumPy array?
A zero-length array has at least one dimension with length zero. For example, np.empty((0,)) has shape (0,) and no elements. np.empty((2, 0)) has shape (2, 0) and also no elements: the zero-length dimension leaves no positions to populate. Both are valid arrays, and each retains its shape and dtype metadata. This follows from NumPy’s shape contract: shape specifies the returned array’s dimensions and their lengths. The NumPy array creation guide explains array shapes and creation.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware match#1 Best Overall
Zero-length does not mean “filled with zeros.” It means the array has no entries at all. The distinction matters when checking a shape or passing an array between functions: an array can be valid and have a dtype even when it has no values to read.
Default dtype, shape, and order
The current documented signature is numpy.empty(shape, dtype=None, order='C', *, device=None, like=None). The shape argument may be an integer or a tuple of integers. If dtype is omitted, it defaults to float64; specify dtype= when you need another type. The default memory order is C-style; choose order='F' for Fortran-style order where needed.
The optional device parameter is documented as new in NumPy 2.0.0 and, for Array API interoperability, must be 'cpu' if supplied. The optional like parameter is documented as new in NumPy 1.20.0; when the reference object supports __array_function__, it can determine a compatible output type. Consult the API reference for details applicable to your installed NumPy version.
Examples: zero-length arrays and safe assignment
import numpy as np
# Zero elements; dtype defaults to float64
x = np.empty((0,))
# Zero elements; explicitly choose an integer dtype
y = np.empty((3, 0), dtype=np.int32)
# Nonzero array: assign values before reading them
z = np.empty(3, dtype=np.float64)
z[:] = [1.0, 2.0, 3.0]
# Use zeros when zero initialization is required
safe_start = np.zeros(3, dtype=np.float64)
There is one documented exception to the arbitrary-value rule: object arrays returned by empty are initialized to None. That exception does not make np.empty a zero-initializing constructor for other dtypes.
np.empty vs. np.zeros and other constructors
| Need | Constructor | What it provides |
|---|---|---|
| Allocate an array that your code will overwrite in every position | np.empty |
Requested shape and dtype without initializing ordinary entries; write before reading. |
| Start with zero-valued entries | np.zeros |
Requested shape filled with zeros. See the NumPy zeros reference. |
| Use a prototype array as the model for shape and type | np.empty_like |
A creation routine that takes a prototype array; see NumPy’s array creation routines. |
| Initialize with ones or a chosen constant | np.ones or np.full |
Constructors for one-filled arrays or arrays filled with a chosen value; see NumPy’s array creation routines. |
Choose based on whether initialization is required, whether your code reliably overwrites every slot, the dtype and shape you need, and the desired memory order. NumPy’s manual notes a possible marginal speed advantage from skipping initialization, but that is not a performance guarantee; no measured benchmark is specified here.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




