How to create an empty NumPy array
There are two ways an empty NumPy array can be created: numpy.zeros and numpy.empty. Both of these methods differ slightly, as shown below:
Syntax
The syntax for using numpy.zeros and numpy.empty is shown below:
numpy.zeros(shape, dtype=float, order='C')
numpy.empty(shape, dtype=float, order='C')
# Shape -> Shape of the new array, e.g., (2, 3) or 2.
# dtype -> The desired data-type for the array,e.g., numpy.int8. Default is numpy.float64. This parameter is optional.
# order -> Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory. This parameter is optional.
Code
Consider the code snippet below that demonstrates how to use numpy.zeros to create an empty NumPy array:
import numpy as npmyArr = np.zeros((2,3))print(myArr)
The code snippet below demonstrates how to use numpy.empty to create an empty NumPy array:
import numpy as npmyArr = np.empty((2,3))print(myArr)
numpy.empty, unlikenumpy.zeros, does not set the array values to zero and, therefore, may be marginally faster. On the other hand, it requires the user to manually set all the values in the array and should be used with caution.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved