Create Empty NumPy Array

  1. Create an Empty NumPy Array With the numpy.zeros() Function
  2. Create an Empty NumPy Array With the numpy.empty() Function in Python

In this tutorial, we will discuss methods to create an empty NumPy array in Python.

Create an Empty NumPy Array With the numpy.zeros() Function

The NumPy package is used to perform complex calculations on the array data structure in Python. Due to the Object-Oriented nature of Python, no variable can be truly empty. We can fill an array with zeros to call it empty in Python. The numpy.zeros() function is used to fill an array with zeros. The following code example shows us how to create an empty array with the numpy.zeros() function.

import numpy as np
a = np.zeros(shape=5)
print(a)

Output:

[0. 0. 0. 0. 0.]

In the above code, we created an empty array that contains five elements with the numpy.zeros() function in Python. We specified the shape, i.e., the number of rows and columns of the array with the shape parameter inside the numpy.zeros() function.

Create an Empty NumPy Array With the numpy.empty() Function in Python

We can also use the numpy.empty() function to create an empty numpy array. Since nothing can be empty in Python, the numpy.empty() function also fills all the array elements with zeros. The following code example shows us how to create an empty array with the numpy.empty() function.

import numpy as np
a = np.empty(shape=5)
print(a)

Output:

[0. 0. 0. 0. 0.]

In the above code, we created an empty array that contains five elements with the numpy.empty() function in Python. We specified the shape, i.e., the number of rows and columns of the array with the shape parameter inside the numpy.empty() function.

As it is clear from the above examples, both methods are very similar and do the same thing. We can choose any of the above-discussed methods in a scenario where we need an empty NumPy array, and it won’t make any difference.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.