# 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)``

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]]``````

### 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.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)``

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])
print(updated_temperatures)``````

Output: The implementation of the Python code:

``[72 74 71 68 75 76 70 73]``

### 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']]``````

## 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:

READ:  Working with Python Lil_Matrix Scipy

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]``````

## 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.