Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

NumPy Empty Arrays: np.empty(), Zero-Length Shapes, and dtype

NumPy’s np.empty() creates an array without initializing ordinary values. Learn how zero-length shapes, dtype defaults, and np.zeros differ.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.