How to Append to an Empty Array in Numpy (with Examples)

Let’s see how to append to an empty array in the Numpy Python module.
numpy reshape array

Numpy is a Python library that provides a high-level interface to arrays. It is a powerful tool for scientific computing and data analysis.

One of the things that you can do with Numpy is to append values to an empty array. This can be useful for a variety of tasks, such as adding new data to a dataset or creating a new array from a list of values.

A little reminder. We already discussed how to create an empty array in Numpy. We used the following code:

import numpy as np

empty_array = np.empty((3, 3))

print(empty_array)

How to append to an empty array?

To append to an empty array in Numpy, you can use the append() function. The append() function takes two arguments: the first argument is the empty array, and the second argument is the value or values that you want to append to the array.

See also  How to generate evenly spaced sample in Numpy?

To append to an empty array, just use the append Numpy function.

import numpy as np

empty_array = np.empty((3, 3))
new_array = np.append(empty_array, 6)

print(new_array)

numpy append empty array python

Using the append method, I’ve added six.

To append the empty array more and add more values, just use parentheses.

import numpy as np

empty_array = np.empty((3, 3))
new_array = np.append(empty_array, (6, 3))

print(new_array)

numpy append empty array many values python

This is what your array looks like. If you don’t like what you see, you can reshape it now.

import numpy as np

empty_array = np.empty((3, 3))
new_array = np.append(empty_array, (6, 3, 4))
reshape = new_array.reshape(4, 3)

print(reshape)

numpy reshape array

How to append to an empty array from another array?

Going further, you can also append an empty array with the values of another Numpy array. Python code would be like that:

import numpy as np

empty_array = np.empty((3, 3))
values = [1, 2, 3]
new_array = np.append(empty_array, np.array([values]))

print(new_array)

As an output, an empty array has been appended by 1,2,3 values as expected.

[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 2. 3.]

Appending Multiple Values to an Empty Array

Use the append() function and pass a list of values as the second argument. For example, the following code appends the values 6, 3, and 4 to an empty array:

import numpy as np

empty_array = np.empty((3, 3))

new_array = np.append(empty_array, [6, 3, 4])

print(new_array)

The values 6, 3, and 4 have been appended to the empty array.

See also  How to generate array filled with value in Numpy?

We can also use the append() function to append multiple Numpy arrays to an empty array. For example, the following code creates two Numpy arrays, one with the values 1, 2, and 3, and the other with the values 4, 5, and 6. It then appends these two arrays to an empty array:

import numpy as np

array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 5, 6])

empty_array = np.empty((3, 3))

new_array = np.append(empty_array, [array_1, array_2])

print(new_array)

The two Numpy arrays have been appended to the empty array.

See also  How to generate Cauchy Matrix from arrays in Numpy?

Appending the Values of Another Numpy Array to an Empty Array

Use the append() function and pass the Numpy array as the second argument. For example, the following code creates a Numpy array with the values 1, 2, and 3, and then appends it to an empty array:

import numpy as np

array_1 = np.array([1, 2, 3])

empty_array = np.empty((3, 3))

new_array = np.append(empty_array, array_1)

print(new_array)

The values of the Numpy array have been appended to the empty array.