How to Append NumPy Arrays Examples

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 13, 2023
  • Reading time:18 mins read

numpy.append() is used to append two or multiple arrays at the end of the specified NumPy array. The NumPy append() function is a built-in function in the NumPy package of Python. This function returns a new array after appending the array to the specified array by keeping the original array unchanged.

The numpy.append() appends values along the mentioned axis at the end of the array. In this article, I will explain how to create NumPy arrays using numpy.array(), and then append arrays using append() function with examples. Depending on your usage see if you want to concatenate NumPy arrays.

1. Quick Examples of Append NumPy Arrays

If you are in a hurry, below are some quick examples of how to append Python NumPy arrays.


# Quick wxamples of append numpy arrays

# Example 1: Creating a 1D NumPy array 
# From a Python list
my_list = [0, 2, 4, 6, 8]
result = np.array(my_list)

# Example 2: Create 2D numpy array 
# Using numpy.array() method
my_list = [[0,2,4],[6,8,10]]
result = np.array(my_list) 

# Example 3: Use numpy.append() function
arr = np.array([0,2,4,6,8,10]) 
result = np.append(arr, [13,15,17]) 

# Example 4: Appending arrays 
# Along axis=0 (stacking vertically)
arr = np.array([0, 1, 3])
arr1 = np.array([5, 7, 9])
result = np.append(arr, arr1, axis=0)

# Example 5: Creating two 2D NumPy arrays
arr = np.array([[0,2,4],[6,8,10]]) 
result = np.append(arr, [[5,7,9]],axis = 0)

# Example 6: Appending arrays 
# Along axis=1 (stacking horizontally)
arr = np.array([[0,2,4],[6,8,10]]) 
result = np.append(arr, [[5,7,9],[13,15,17]],axis = 1) 

# Example 7: Use arange() Function 
# To Append an Array
array = np.arange(1, 6)  
array1 = np.array([6, 7, 8, 9, 10])
result = np.append(array, array1)

2. Syntax of NumPy Array append()

Following is the syntax of the NumPy array append() function. This function internally uses the NumPy concatenate() function.


# Syntax of numpy.append()
numpy.append(array, values, axis)

2.1 Parameters of append()

This method allows three parameters :

  • array – Input array, new values are appended to a copy of this array. This parameter is required and plays an important role in numpy.append() function.
  • values – To be appended/added to the array. It must be of the same shape as of array. (excluding axis of appending). If the axis is not defined, then the values can be in any shape and will flatten before use.
  • axis – Axis along which you want to insert the values. By default, the array is flattened.

2.2 Return Value of append()

The return value of this function is the NumPy array which is the copy of the array with the appended passed values to the axis.

3 Create a NumPy Array

To create a NumPy array, you can use the numpy.array() function. You can pass a Python list, tuple, or any array-like object to this function, and it will create a NumPy array.

In order to work with an example, first, let’s create a NumPy array using numpy.array() method. Before going to create the NumPy array we have to import the NumPy module.

3.1 Creating a 1D NumPy Array from a Python List

Creating a 1D NumPy array from a Python list is a common operation. You can use the numpy.array() function to achieve this. For instance, the numpy.array() function takes the Python list my_list and converts it into a 1D NumPy array result. You can then perform various operations result using NumPy functions and methods.


# Import NumPy
import numpy as np

# Creating a 1D NumPy array 
# From a Python list
my_list = [0, 2, 4, 6, 8]
result = np.array(my_list)
print("After appending numpy arrays:\n", result)

Yields below output.

NumPy arrays append

3.2 Creating a 2D NumPy Array from a Python List of Lists

Creating a 2D NumPy array from a Python list of lists involves using the numpy.array() function with a list of lists as the argument. Each inner list represents a row of the 2D array.

In the below example, my_list is a Python list containing two inner lists. Each inner list represents a row of the resulting 2D NumPy array. The np.array() function converts this list of lists into a 2D NumPy array called result. You can see that the structure of the resulting NumPy array matches the structure of the input list of lists.


# Create 2D numpy array 
# Using numpy.array() method
my_list = [[0,2,4],[6,8,10]]
result = np.array(my_list) 
print("After appending numpy arrays:\n", result)

Yields below output.

NumPy arrays append

3. Usage of NumPy Array append()

You can use numpy.append() function to add an element in a NumPy array. You can pass the NumPy array and multiple values as arguments to the append() function. It doesn’t modify the existing array but returns a copy of the passed array with given values added.


# Use numpy.append() function
arr = np.array([0,2,4,6,8,10]) 
result = np.append(arr, [13,15,17]) 
print("After appending numpy arrays:\n", result)

# Output:
# After appending numpy arrays:
#  [ 0  2  4  6  8 10 13 15 17]

You can observe, that from the above code, I have imported NumPy with the alias name np. I have created an array arr using the np.array() function, and also I have taken another array. Then I applied the NumPy array append() function for both two arrays. After that, I assigned a value of the append() function to the result variable. Finally, we have got a flattened array.

3.1 Append NumPy Arrays Along with axis = 0

When you want to append NumPy arrays along axis=0, you are essentially stacking them vertically, meaning you’re adding rows to the arrays.

In the below example, arr and arr1 are both 1D arrays. By specifying axis=0 in np.append(), the two arrays are appended vertically, resulting in a new 1D array [0 1 3 5 7 9].


# Creating two 1D NumPy arrays
arr = np.array([0, 1, 3])
arr1 = np.array([5, 7, 9])

# Appending arrays along axis=0 (stacking vertically)
result = np.append(arr, arr1, axis=0)
print("After appending arrays along axis 0:\n", result)

# Output:
# After appending arrays along axis 0:
#  [0 1 3 5 7 9] 

If you have 2D arrays and want to append them along axis=0, you can use the same approach. In code, two 2D NumPy arrays are created, and then a new row [5, 7, 9] is appended to the original array using np.append() along axis=0.

In the below output, the new row [5, 7, 9] has been successfully appended to the original 2D array along axis=0, resulting in a new 2D array with the added row.


# Creating two 2D NumPy arrays
arr = np.array([[0,2,4],[6,8,10]]) 
result = np.append(arr, [[5,7,9]],axis = 0)
print("After appending arrays along axis 0\n", result)

# Output:
# After appending arrays along axis 0:
#  [[ 0  2  4]
#  [ 6  8 10]
#  [ 5  7  9]]

3.2 Append Arrays Along with axis = 1

Similarly, you can also use the numpy.append() function to append arrays along axis=1. For instance, two arrays [[5, 7, 9], [13, 15, 17]] are appended to the original array [[0, 2, 4], [6, 8, 10]] along axis=1, resulting in a new 2×6 array. The arrays are concatenated horizontally, adding columns to the original array.


# Appending arrays along axis=1 (stacking horizontally)
arr = np.array([[0,2,4],[6,8,10]]) 
result = np.append(arr, [[5,7,9],[13,15,17]],axis = 1) 
print("After appending arrays along axis 1:\n", result)

# Output:
# After appending arrays along axis 1:
#  [[ 0  2  4  5  7  9]
#  [ 6  8 10 13 15 17]]

As in the above program, the append() function is used to append 2-Darray with 2-D array(horizontally). The appended arrays we can see in the output, the append is done horizontally as the axis value is 1.

4. Use arange() Function to Append an Array

You can use the numpy.arange() function to create an array with a sequence of numbers and then append another array to it using the numpy.append() function.

In the below example, np.arange(1, 6) creates an array from 1 to 5, and then the np.append() function is used to append the array1 to the array, resulting in a new array [1 2 3 4 5 6 7 8 9 10].


# Creating an initial array 
# Using arange()
array = np.arange(1, 6)  
print("First array:\n", array)

# Creating an array to append
array1 = np.array([6, 7, 8, 9, 10])
print("Second array:\n", array1)

# Appending the arrays
result = np.append(array, array1)
print("After appending numpy arrays:\n", result)

Yields below output.


# Output:
# First array:
#  [1 2 3 4 5]
# Second array:
#  [ 6  7  8  9 10]
# After appending numpy arrays:
#  [ 1  2  3  4  5  6  7  8  9 10]

Frequently Asked Questions Append NumPy Arrays

What does the numpy.append() function do?


The numpy.append() function is used to append values to the end of an existing array. It creates a new array with the specified values appended to the original array.

Does numpy.append() modify the original array?

numpy.append() does not modify the original array. It creates a new array with the appended values and returns the new array.

How can I append arrays along different axes?

You can specify the axis parameter in numpy.append() to control the axis along which the appending should be done. For example, axis=0 appends along rows (vertical stack), and axis=1 appends along columns (horizontal stack).

Can I append arrays of different data types?

You can append arrays of different data types in NumPy using the numpy.append() function. When you append arrays of different data types, NumPy will automatically upcast the data types to a common type that can accommodate all the data. NumPy has a set of rules for determining the resulting data type, which is generally based on the concept of type promotion.

Can I append multiple values at once using numpy.append()?

You can append multiple values at once using the numpy.append() function. When you want to append multiple values, you can pass a sequence (like a list, tuple, or another array) containing the values you want to append.

5. Conclusion

In this article, I have explained how to append NumPy arrays by using numpy.append() methods with examples. When you append NumPy arrays, it doesn’t change the existing array instead it creates a new array by adding an array to the end of the specified array.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply