Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

empty
numpy
array
c

How to create an empty NumPy array

Educative Answers Team

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:

svg viewer

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 np
myArr = 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 np
myArr = np.empty((2,3))
print(myArr)

numpy.empty, unlike numpy.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.

RELATED TAGS

empty
numpy
array
c
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring