Software, Tech & Coding simplified.

numpy.append(): How to Add Elements to a NumPy Array

A NumPy array does not have a built-in append method. Instead, to append elements to a NumPy array, use a separate numpy.append() function.

For example:

import numpy as np

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

arr = np.append(arr, 4)

print(arr)

Output:

[1 2 3 4]

Notice how numpy.append() creates a new copy of the original array. It does not directly append values to it.

In this guide, you learn:

  • How appending NumPy arrays work.
  • How concatenating NumPy arrays work.
  • What is the difference between concatenating and appending.

How to Append to a NumPy Array

Appending to an array means adding a value or values to the end (right side) of the array.

Intuitively, appending means modifying the original array by adding the element to it.

appending array intuition

However, when it comes to NumPy arrays, appending works slightly differently than you would expect.

There is a function called numpy.append() you can use to append elements to an array.

The full syntax of this function is:

numpy.append(arr, values, axis=None)

Where:

  • arr is the original array to which you are appending
  • values are appended to the copy of arr.
  • axis is the axis along which the values are appended. This is an optional value that is None by default.

Notice how the numpy.append() function does not directly append the values into the array.

Instead, the result is a completely new array that is a copy of the original array with the appended element(s).

Here is an illustration:

numpy array append illustration
Appending creates a new array.

For example:

import numpy as np

arr = np.array([1, 2, 3])
arr = np.append(arr, 4)

print(arr)

Output:

[1 2 3 4]

Here we assign the resulting array of the append() function call back to the original array arr.

If we did not do this, there would be no appended value at the end of the array.

import numpy as np

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

# Does not work modify the original array 'arr'
np.append(arr, 4)

print(arr)

Output:

[1 2 3]

Now you understand how to append elements to NumPy arrays.

Next, let’s take a look at how you can append an array to the end of another.

How to Append a NumPy Array to Another

numpy append array to another
Appending array to another merges the two.

Appending a NumPy array at the end of another NumPy array works using the numpy.append() method.

arr.append(arr1, arr2)

Where the elements in arr2 are appended to arr1.

For example:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

arr1 = np.append(arr1, arr2)

print(arr1)

Output:

[1 2 3 4 5 6]

Now you understand how to append both singular values and entire arrays to the end of NumPy arrays.

Lastly, let’s have a look at another approach, concatenation, which you are going to see a lot.

Append Alternative: Concatenation

You can also use numpy.concatenate() function to add elements to the end of an array.

To do this, pass the element/array arguments as a sequence into the concatenate() function call.

Notice that the dimensions of the arguments must match.

In other words, you cannot for example concatenate a single value to the end of the array. Instead, you would have to put that value into an array or list to make the dimensions match first.

Let’s see some examples of concatenation.

For instance, let’s add an array of numbers, arr2, to the end of another array of numbers, arr1:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

arr1 = np.concatenate((arr1, arr2))

print(arr1)

Output:

[1 2 3 4 5 6]

Similar to appending a NumPy array, concatenation does not modify the original array!

Instead, the numpy.concatenate() function creates a new copied array with the concatenated elements.

As another example, let’s add a single number to the array of numbers.

Because a single value and an array has different dimensions, this is not directly possible. Instead, put the single value into the list (of the same dimensions as the array) to make the dimensions match.

Here is how it looks in code:

import numpy as np

arr1 = np.array([1, 2, 3])
arr1 = np.concatenate((arr1, [4]))

print(arr1)

Output:

[1 2 3 4]

Now you know two ways to add elements/arrays to the end of another NumPy array.

Finally, let’s make a quick comparison between appending and concatenating.

numpy.append() vs numpy.concatenate()

The difference between numpy.append() and numpy.concatenate() is that numpy.append() uses numpy.concatenate() behind the scenes.

You can view the implementation of numpy.append() in the official implementation.

Anyway, the implementation of numpy.append() looks like this:

def append(arr, values, axis=None)
    arr = asanyarray(arr)
    if axis is None:
        if arr.ndim != 1:
            arr = arr.ravel()
        values = ravel(values)
        axis = arr.ndim-1
    return concatenate((arr, values), axis=axis)

As you can see, the last line produces a result by calling the numpy.concatenate() function.

Conclusion

Today you learned how to append to a NumPy array.

To recap, use the numpy.append() function to add elements to the end of an array. However, keep in mind this does not actually add the elements to the array. Instead, it creates a copy of the array.

Thanks for reading.

Happy coding!

Further Reading

Best Python Data Science Courses

Share

Share on twitter
Share on linkedin
Share on facebook
Share on pinterest
Share on email