AppDividend
Latest Code Tutorials

# How to Create Numpy Empty Array in Python

Numpy empty() function is used to create a new array of given shape and type, without initializing entries. Numpy empty, unlike zeros() method, does not set array values to zero, and may, hence, be marginally faster. On the other side, it requires the user to set all the values in the array manually and should be used with caution. A Numpy array is a very diverse data structure from a list and is designed to be used in different ways.

## Understanding Numpy array

Numpy array is the central data structure of the Numpy library. On a structural level, an array is nothing but pointers. It’s a combination of the memory address, data type, shape, and strides. To make a numpy array, you can just use the np.array() function. All you need to do is pass a list to it, and optionally, you can also specify the data type of the data.

```import numpy as np

list = ['Python', 'Golang', 'PHP', 'Javascript']
arr = np.array(list)
print(arr)
```

#### Output

`['Python' 'Golang' 'PHP' 'Javascript']`

As you can see in the output, we have created a list of strings and then pass the list to the np.array() function, and as a result, it will create a numpy array.

## Create Numpy Empty Array

To create a numpy empty array, we can pass the empty list to the np.array() function, and it will make the empty array.

```import numpy as np

list = []
arr = np.array(list)
print(arr)```

#### Output

`[]`

You can see that we have created an empty array using np.array().

We can also check its data type.

```import numpy as np

list = []
arr = np.array(list)
print(arr.dtype)
```

#### Output

`float64`

## Using np.empty() method

The numpy.empty(shape, dtype=float, order=’C’) returns a new array of given shape and type, without initializing entries.

To create an empty array in Numpy (e.g., a 2D array m*n to store), in case you don’t know m how many rows you will add and don’t care about the computational cost then you can squeeze to 0 the dimension to which you want to append to arr = np.empty(shape=[0, n]).

```import numpy as np

arr = np.empty([0, 2])
print(arr)
```

`[]`

## How to initialize Efficiently numpy array

NumPy arrays are stored in the contiguous blocks of memory. If you need to append rows or columns to an existing array, the entire array needs to be copied to the new block of memory, creating gaps for the new items to be stored. This is very inefficient if done repeatedly to create an array.

In the case of adding rows, this is the best case if you have to create the array that is as big as your dataset will eventually be, and then insert the data to it row-by-row.

```import numpy as np

arr = np.zeros([4, 3])
print(arr)
```

#### Output

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

And then, you can add the data of row by row, and that is how you initialize the array and then append the value to the numpy array.

## Conclusion

To create an empty numpy array, you can use np.empty() or np.zeros() function. Both can be helpful.