NumPy concatenate vs append in Python [4+ examples]

Do you want to know what is the difference between NumPy concatenate vs append in Python? In this NumPy Python article, I will explain what the NumPy concatenate is and what the NumPy append is with illustrative examples. Also, I have created a table of the key difference between NumPy concatenate vs append in Python.

The main difference between np.concatenate and np.append in Python NumPy is that np.concatenate is primarily used for concatenating multiple arrays along specified axes, allowing for more complex array manipulation, while np.append is designed for appending elements to the end of an existing 1D NumPy array.

np.concatenate in Python

The np.concatenate function in NumPy is designed for concatenating arrays along specified axes in Python. It is a versatile tool for combining arrays of the same shape and is particularly useful when we need to join multiple arrays along a specific axis, such as rows (axis 0) or columns (axis 1).

Here’s the basic syntax of np.concatenate() function in Python:

np.concatenate((arr1, arr2, ...), axis=0)
NameDescription
arr1, arr2, and so onThey are the arrays we want to concatenate in Python.
axisThis specifies the axis along which the concatenation should occur. By default, it concatenates along axis 0 (rows).
List of parameters required in the np.concatenate() function in Python.

Let’s look at a few examples to understand how np.concatenate in Python works:

Example 1: NumPy concatenate along rows(default axis = 0) in Python

In this instance, we have two 2D arrays in Python. and we are concatenating one of the arrays to the other and creating a new NumPy Python array.

import numpy as np

state1_population = np.array([[1000000, 500000], [300000, 200000]])
state2_population = np.array([[750000, 400000]])
usa_population = np.concatenate((state1_population, state2_population))
print(usa_population)

Output: The implementation of the code in the Python Pycharm editor

[[1000000  500000]
 [ 300000  200000]
 [ 750000  400000]]
numpy concatenate two 1d arrays in Python

Example 2: NumPy concatenate multiple arrays in Python along columns (axis = 1)

Let’s try to concatenate two NumPy arrays in Python.

import numpy as np

state1_gdp = np.array([[50000, 55000, 60000]])
state2_gdp = np.array([[63000, 64000, 68000]])
usa_gdp = np.concatenate((state1_gdp, state2_gdp), axis=1)
print(usa_gdp)

Output: The implementation of the code with a screenshot in Python.

[[50000 55000 60000 63000 64000 68000]]
np concatenate vs append in Python

np.append in Python

On the other hand, np.append is a simpler function that appends values to an existing NumPy array along a specified axis in Python. While it can be used for concatenation, it is more suitable for adding individual elements or arrays to the end of an existing array in Python.

Here’s the syntax of the numpy.append() function in Python:

np.append(arr, values, axis=None)
NameDescription
arrThe input NumPy array in Python.
valuesThe values to append to arr through Python.
axis(optional) The axis along which to append the values. If not specified, the array is flattened before appending.
List of parameters required in the np.append() function in Python.

Here are some examples to illustrate what the np.append does:

Example 1: NumPy append two 1d arrays in Python

Let’s take two arrays and try to append the value of one numpy array to another’s end through Python.

import numpy as np

temperatures = np.array([72, 74, 71, 68, 75, 76, 70])
additional_temperature = np.array([73])
updated_temperatures = np.append(temperatures, additional_temperature)
print(updated_temperatures)

Output: The implementation of the Python code:

[72 74 71 68 75 76 70 73]
np.append vs np.concatenate in Python

Example 2: Python append multiple arrays

Here, Let’s take two arrays and append the values from one NumPy array to the another through Python np.append() function.

import numpy as np

top_universities = np.array([["Harvard", "MIT", "Stanford"]])
new_university = np.array([["Yale", "Caltech", "Princeton"]])
updated_universities = np.append(top_universities, new_university, axis=0)
print(updated_universities)

Output: The implementation of the code in Python:

[['Harvard' 'MIT' 'Stanford']
 ['Yale' 'Caltech' 'Princeton']]
np append vs concatenate in Python

NumPy concatenate vs append in Python

Now that we have seen examples of both np.concatenate and np.append, let’s summarize the key differences:

Featuresnumpy.concatenatenumpy.append
OperationConcatenates two or more NumPy arrays along the specified axis in Python.Appends values to the end of a NumPy array in Python.
Input ArraysExpects a sequence of arrays to be concatenated.Expect two arrays as input.
Axis ParameterAllows specifying the axis along which to concatenate.No direct control over the axis.
Usage for ArraysCommonly used for concatenating NumPy arrays along an axis.Generally used for appending elements.
PerformanceTypically more efficient for concatenating multiple arrays along an axis in Python.May be less efficient when appending to the end of an array multiple times in Python.
In-Place OptionCan be used in-place by assigning the result to an existing Python NumPy array.Does not provide the in-place option.
Multiple AxesSupports concatenation along multiple axes simultaneously.Only appends elements to the end of a Python NumPy array.
FlexibilityOffers more control and flexibility in concatenation operations.Simpler but less versatile for array manipulation.
ExamplePython np.concatenate([arr1, arr2], axis=0)Python np.append(arr1, arr2)
List of key differences between NumPy concatenate vs append in Python.
numpy concatenate vs append

These are the key differences between NumPy concatenate vs append in Python.

np.concatenate vs np.append in coding

Here, I have taken one example for each np.concatenate() and np.append() function simultaneously to make you understand easily:

import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
result = np.concatenate([arr1, arr2], axis=0)
print('Result for concatenate:\n', result)
import numpy as np

arr1 = np.array([1, 2, 3])
element1 = 4
element2 = 5
result = np.append(arr1, [element1, element2])
print('Result for append:\n', result)

The Outputs are:

Result for concatenate:
 [[1 2]
 [3 4]
 [5 6]]
Result for append:
 [1 2 3 4 5]
numpy append vs concatenate in Python

Conclusion

Understanding what is the difference between NumPy concatenate vs append in Python? The np.concatenate is suitable for more complex array concatenation operations, while np.append is designed for simpler tasks like adding elements to the end of an array. The choice between them depends on the specific task one needs to perform.

You may also like to read: