Numpy: use np.append() and np.insert() to add lists to ndarray

In this recipe we’ll learn how to add numeric lists into NumPy ndarrays. We’ll look into two cases:

  1. appending a Python list to the end an existing array (which oculd be either 1d / 2d or more).
  2. Insert a list into a specific position in the array

Use np.append() to concatenate a list and an array

We’ll use a simple 1d array as an example. Let’s start by importing Numpy.

#Python 3
import numpy as np

first_array = np.array([1265, 1125, 1996, 1527, 1320])
print(first_array)

Here’s our example array:

array([1265, 1125, 1996, 1527, 1320])

first_array.size

#will return 5

Now we’ll define the list that will be appended:

my_list = [2500, 2700, 2900]

Using np.append joins the list at the end of our array:

sec_array = np.append(first_array, my_list)
The size of sec_array will be 8:
array([1265, 1125, 1996, 1527, 1320, 2500, 2700, 2900])

Note: The first parameter is the source array, the second is the array that we want to add. Note that both should have the same shape dimension. Optionally you’ll need to specify also also the axis, otherwise the arrays are flattened by default becfore being concatenated.

np.insert to add a list into an array

Consider this code:

third_array = np.insert(first_array, my_list)

This is going to throw the following error:

TypeError: _insert_dispatcher() missing 1 required positional argument: ‘values’

The reason is that unlike np.append(), np.insert expects three parameters: the source array, the position in which we’ll insert the new array and the new array. Optionally you’ll need to specify also also the axis.

third_array = np.insert(first_array, 3, my_list)
print(third_array)

Will result in:

array([1265, 1125, 1996, 2500, 2700, 2900, 1527, 1320])

That’s pretty much it, leave us a comment in case of questions.

Quick Tips and Answers

Why does np.append() create a new array instead of modifying the original?

NumPy arrays have a fixed size in memory once created, so np.append() allocates a brand-new array, copies existing elements, and adds the new values at the end. This means repeated appending in a loop is inefficient because each call triggers a full copy. A better pattern is to collect all your data in a Python list first, then convert once with np.array(). If your NumPy installation fails before you even get to this point, check our guide on fixing the ModuleNotFoundError for NumPy to resolve environment issues quickly.

How do I append a list to a NumPy array along a specific axis?

Use the axis parameter in np.append() or, for more control, use np.vstack() and np.hstack(). For row-wise appending to a 2D array, np.vstack([arr, new_row]) is clearer than np.append(arr, [new_row], axis=0). Both require the dimensions to match exactly. When you need to move data between Pandas and NumPy structures, you can convert a Pandas index to a list or array before appending it to your target array, ensuring type compatibility throughout the process.

What is the performance difference between np.append() and np.concatenate()?

Under the hood, np.append() calls np.concatenate(), so there is no meaningful speed difference for a single operation. The real performance gain comes from batching: call np.concatenate() once with a list of arrays rather than calling np.append() inside a loop. For 10,000 appends, the batched approach can be over 100 times faster because it allocates memory only once instead of 10,000 times.

Leave a Comment