In this article we will discuss different ways to create an empty 1D,2D or 3D Numpy array and of different data types like int or string etc.

Python’s numpy module provides a function empty() to create new arrays,

numpy.empty(shape, dtype=float, order='C')

  • It accepts shape and data type as arguments.
  • Returns a new array of given shape and data type but without initializing entries. It means the returned numpy array will contain garbage values.
  • If data type argument is not provided then the default data type of all entries in the returned numpy array will be float.

Let’s use this empty() function to create an empty numpy array of different shape and data types.

Create an empty 1D Numpy array of given length

To create an 1D Numpy array of length 5, we need pass a the integer 5 as shape argument to the empty() function,

# Create an empty 1D Numpy array of length 5
empty_array = np.empty(5)

print(empty_array)

Output:
[0.00000000e+000 0.00000000e+000 0.00000000e+000 2.96439388e-321
 3.10386678e-304]

It returned an empty array of 5 floats with garbage values.

Create an empty Numpy array of given shape using numpy.empty()

In the previous example, we create an empty 1D numpy array. Let’s see how to create 2D and 3D empty Numpy array using empty() function,

Create an empty 2D Numpy array using numpy.empty()

To create an empty 2D Numpy array we can pass the shape of the 2D array ( i.e. row & column count) as a tuple to the empty() function.
Let’s create a empty 2D Numpy array with 5 rows and 3 columns,

# Create an empty 2D Numpy array or matrix with 5 rows and 3 columns
empty_array = np.empty((5, 3))

print(empty_array)

Output:
[[3.04317917e-253 1.25368622e-253 7.08611886e-293]
 [7.58252790e-293 7.08734195e-293 2.66107716e-304]
 [1.22209808e-253 1.25384233e-253 1.22216054e-253]
 [1.25392036e-253 1.22222298e-253 1.26817172e-253]
 [1.22228542e-253 2.86005439e-304 1.75451605e-292]]

It returned an empty 2D Numpy Array of 5 rows and 3 columns but all values in this 2D numpy array were not initialized.

As we did not provided the data type argument (dtype), so by default all entries will be float.

Create an empty 3D Numpy array using numpy.empty()

To create an empty 3D Numpy array we can pass the shape of the 3D array as a tuple to the empty() function.
Let’s create a empty 3D Numpy array with 2 matrix of 3 rows and 3 columns,

# Create an empty 3D Numpy array
empty_array = np.empty((2, 3, 3))

print(empty_array)

Output:
[[[6.23042070e-307 4.67296746e-307 1.69121096e-306]
  [1.60218491e-306 1.02356521e-306 6.23053614e-307]
  [2.22526399e-307 6.23053614e-307 7.56592338e-307]]

 [[1.60216183e-306 7.56602523e-307 3.56043054e-307]
  [1.37961641e-306 2.22518251e-306 1.33511969e-306]
  [1.05694828e-307 3.11521884e-307 5.72778079e+252]]]

It returned an empty 3D Numpy Array with 2 matrices of 3 rows and 3 columns, but all values in this 3D numpy array were not initialized.

In all the above examples, we didn’t provide any data type argument. Therefore by default float data type was used and all elements were of float data type. But it might be possible that in some scenarios you want to create empty numpy arrays of other data types. Let’s see how to do that,

Create an empty Numpy array with custom data type

To create an empty numpy array of some specific data type, we can pass that data type as a dtype argument in the empty() function.
Let’s understand with some examples,

Create an empty Numpy array of 5 Integers

To create an empty numpy array of 5 integers, we need to pass int as dtype argument in the numpy.empty() function,

# Create an empty Numpy array of 5 integers
empty_array = np.empty(5, dtype=int)

print(empty_array)

Output:
[1864397668 1752637550 1981838433 1769173605 1864396399]

Create an empty Numpy array of 5 Complex Numbers

To create an empty numpy array of 5 complex numbers, we need to pass complex as dtype argument in the numpy.empty() function,

# Create an empty Numpy array of 5 Complex Numbers
empty_array = np.empty(5, dtype=complex)

print(empty_array)

Output:
[2.84668658e-305+7.47339554e-254j 7.47381179e-254+7.47422804e-254j
 7.47464429e-254+7.28530281e-254j 7.47485241e-254+7.47506054e-254j

Create an empty Numpy array of 5 strings

To create an empty numpy array of 5 strings (with size 3), we need to pass ‘S3’ as dtype argument in the numpy.empty() function,

# Create an empty Numpy array of 5 strings of length 3, You also get an array with binary strings
empty_array = np.empty(5, dtype='S3')

print(empty_array)

Output:
[b'\x01' b'' b'' b'' b'\x00\x00\x01']

The complete example is as follows,
import numpy as np


def main():
    print('*** Create an empty Numpy array of given length ***')

    # Create an empty 1D Numpy array of length 5
    empty_array = np.empty(5)

    print(empty_array)

    print('*** Create an empty Numpy array of given shape ***')

    # Create an empty 2D Numpy array or matrix with 5 rows and 3 columns
    empty_array = np.empty((5, 3))
    print(empty_array)

    # Create an empty 3D Numpy array
    empty_array = np.empty((2, 3, 3))
    print(empty_array)

    print('*** Create an empty Numpy array with custom data type ***')

    # Create an empty Numpy array of 5 integers
    empty_array = np.empty(5, dtype=int)
    print(empty_array)

    # Create an empty Numpy array of 5 Complex Numbers
    empty_array = np.empty(5, dtype=complex)
    print(empty_array)

    # Create an empty Numpy array of 5 strings of length 3, You also get an array with binary strings
    empty_array = np.empty(5, dtype='S3')
    print(empty_array)


if __name__ == '__main__':
    main()

Output
*** Create an empty Numpy array of given length ***
[0.00000000e+000 0.00000000e+000 0.00000000e+000 2.52961611e-321
 9.34609789e-307]
*** Create an empty Numpy array of given shape ***
[[2.88497531e-252 1.05963508e-153 6.99416121e-009]
 [1.75004482e+190 9.08967425e+242 3.24263774e-086]
 [5.05808297e-038 6.96320354e-077 6.01389141e-154]
 [6.03095050e-154 6.12134637e-154 1.04884745e-153]
 [1.24395707e-047 9.99004466e+141 2.02566915e-322]]
[[[6.23042070e-307 4.67296746e-307 1.69121096e-306]
  [1.60218491e-306 1.02356521e-306 6.23053614e-307]
  [2.22526399e-307 6.23053614e-307 7.56592338e-307]]

 [[1.60216183e-306 7.56602523e-307 3.56043054e-307]
  [1.37961641e-306 2.22518251e-306 1.33511969e-306]
  [1.05694828e-307 1.11261027e-306 7.01458926e-292]]]
*** Create an empty Numpy array with custom data type ***
[0 0 0 0 0]
[ 1.37961370e-306+4.22795269e-307j  9.34609790e-307+6.23037657e-307j
  3.33774999e-307+1.00132653e-307j  3.11523242e-307+0.00000000e+000j
 -8.69355980e-311+0.00000000e+000j]
[b'\x01\x01\x01' b'\x01\x01\x01' b'\x01\x01\x01' b'\x01\x01\x01'
 b'\x01\x01\x01']

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.