np.append() - How To Use NumPy Append in Python

The NumPy programming library is considered to be a best-of-breed solution for numerical computing in Python.

NumPy stands out for its array data structure. NumPy arrays are excellent for handling ordered data. Moreover, they allow you to easily perform operations on every element of th array - which would require a loop if you were using a normal Python list.

One of the core capabilities available to NumPy arrays is the append method. In this tutorial, I will explain how to use the NumPy append method to add data to a NumPy array.

Table of Contents

You can skip to a specific section of this tutorial using the table of contents below:

How to Import NumPy

This tutorial makes extensive use of the NumPy package for Python. Accordingly, let's start by importing NumPy into our development environment

You can import NumPy under the alias np (which is standard convention) with the following command:

import numpy as np

If you've never used NumPy before, you might be wondering why we import the package under the np alias.

It's because it makes it much easier to reference the package later in our program.

Instead of calling objects and methods from numpy with the dot operator, we can simply call them from np instead.

If this is not clear, do not worry. We will see plenty of examples of this later in this tutorial.

Return to the Table of Contents

What is a NumPy Array?

To understand how to use the np.append method, you first need to understand what a NumPy array is.

NumPy arrays are the main data structure available in the NumPy package. They are similar to normal Python lists, but come with additional functionality.

There are a few different ways that programmers can create NumPy arrays, but the most common is to pass a Python list into the np.array method.

Here is an example:

import numpy as np

my_list = [1, 4, 9, 16]

my_array = np.array(my_list)

You could also pass the list into the np.array method in a single command, like this:

import numpy as np

my_array = np.array([1, 4, 9, 16])

Here's what the my_array object looks like if you print it to the Python console:

array([ 1,  4,  9, 16])

The array() notation indicates that this is indeed a NumPy array.

Return to the Table of Contents

How to Use the NumPy Append Method

Now that you have an understanding of how to create a NumPy array, let's learn about the np.append method.

The append method is used to add a new element to the end of a NumPy array. It accepts two parameters:

  • arr: the array that you'd like to append the new value to.
  • values: the value (or values) that you'd like to append to arr.

Let's consider a few examples to see how the np.append method works in practice.

First, consider the following NumPy array:

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

This NumPy array contains the integers from 1 to 3, inclusive. Let's add 4 to the end of this array using the np.append method:

np.append(first_array, 4)

The np.append method actually returns the value of the new array. In this case, here is the output:

array([1, 2, 3, 4])

In most cases, you will want to store the new array in another variable. This is done like any other variable assignment: using the = assignment operator.

Here is an example:

second_array = np.append(first_array, 4)

You can then reference second_array later in your program, perhaps by using the various NumPy methods and operations that come included in the numerical computing package.

Return to the Table of Contents

How to Append Two NumPy Arrays Together Using np.append

One of the more common use cases of the np.append method is to join two (or more) NumPy arrays together. This section of this tutorial will demonstrate this capability.

For illustration's sake, we will be using the following NumPy arrays;

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

array2 = np.array([4,5,6])

Here's how you would append array2 to the end of array1 using the np.append method:

np.append(array1, array2)

Here is what the output of this code looks like:

array([1, 2, 3, 4, 5, 6])

Similarly, if you wanted to append array1 to the end of the array1, here's how you would do it:

np.append(array2, array1)

In this case, here's the output:

array([4, 5, 6, 1, 2, 3])

It is even possible to append more than three arrays together using np.append. To demonstrate this, I will be using the following 3 arrays:

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

array2 = np.array([4,5,6])

array3 = np.array([7,8,9])

You might think that the following code will properly append the three NumPy arrays together:

np.append(array1, array2, array3)

However, this results in the following error:

TypeError: only integer scalar arrays can be converted to a scalar index

What is the solution?

To append more than two NumPy arrays together using np.append, you must wrap all but the first array in a Python list.

Here is how we would properly append array2 and array3 to array1 using np.append:

np.append(array1, [array2, array3])

Here is the output of this code:

array([1, 2, 3, 4, 5, 6, 7, 8, 9])

For a more extreme example, here's how you would append array2 and array3 twice to the end of array1:

np.append(array1, [array2, array2, array3, array3])

And here is the output of this code:

array([1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9, 7, 8, 9])

Return to the Table of Contents

Final Thoughts

In this tutorial, you learned how to use the np.append method available in the NumPy numerical computing library. You also learned how to append multiple NumPy arrays using np.append.

If you have any other tutorials that you'd like me to write, please email me. I look forward to hearing from you!


Written on May 5th, 2020