HOWTO · NumPy

How to Create Empty NumPy Array

Learn how to create empty NumPy arrays in Python using numpy.zeros() and numpy.empty(). This guide provides clear examples and detailed explanations for each method, helping you efficiently initialize arrays for your data manipulation tasks. Whether you're a beginner or an experienced programmer, you'll find valuable insights to enhance your coding skills.

On this page

Use numpy.zeros() when each element should start at 0. Use numpy.empty() when you will assign every element before reading it. Despite its name, numpy.empty() still creates the requested number of elements; their initial values are simply not set for you. The shape argument describes the array’s dimensions: a single integer such as 5 creates a one-dimensional array, while a tuple such as (3, 4) creates three rows and four columns.

Create a zero-filled array with numpy.zeros()

numpy.zeros() allocates an array and initializes all of its elements to 0. Pass an integer for a one-dimensional array or a tuple for multiple dimensions. By default, NumPy uses a floating-point dtype, so the displayed values may include decimal points. Pass dtype=int when you want integer elements instead. Choose this function when a known initial value is useful—for example, when later calculations update only some positions and the remaining positions should retain 0.

This example creates both a one-dimensional array and a two-dimensional integer array, then updates one position. It prints the values and the tuple returned by the array’s shape attribute.

Run this example:

import numpy as np

values = np.zeros(5)
matrix = np.zeros((3, 4), dtype=int)
values[2] = 7

print(values)
print(matrix.shape)
[0. 0. 7. 0. 0.]
(3, 4)

Because numpy.zeros() initializes every position, it is also appropriate when you plan to fill the array gradually. The initial dtype matters: an integer array stores integer values, while a floating-point array can represent fractional values. The shape parameter always specifies dimensions; it does not inherently mean “rows and columns.” A one-item shape such as 5 has one dimension, and (3, 4) has two.

Create an uninitialized array with numpy.empty()

For ordinary numeric dtypes, numpy.empty() creates an array with the requested shape but does not initialize its elements, so their initial values are unspecified. Object dtype is different: for example, np.empty(2, dtype=object) initializes both elements to None. Assign a valid value before reading any element, and do not depend on the initial contents of numeric arrays.

Use this function when your program can fill every element immediately and avoiding the initialization pass is useful for its workload. Whether that saves noticeable time depends on the array size, the later assignments, the hardware, and the surrounding work; there is no universal speed advantage. If you need reliable starting values or only update some positions, use numpy.zeros() instead.

The following example assigns a value to every position before reading the array. You can change the dimensions or dtype while keeping the same rule: write all elements before using them.

import numpy as np

values = np.empty((2, 3), dtype=int)
values[:] = [[1, 2, 3], [4, 5, 6]]

print(values)
[[1 2 3]
 [4 5 6]]

For a one-dimensional array, the same approach is values = np.empty(5, dtype=int) followed by an assignment to all five positions before any read. A tuple shape works the same way for more dimensions. The name numpy.empty() describes the absence of initialization, not an array with no elements; its shape and element count are still determined by the argument you provide.