DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #9: Array Join

Let say I have two arrays and I want to create the third array that store the combination of values from the previous two arrays, how can I do that?


Join

NumPy has provided a method called join to join two arrays. The join method requires an argument in which you have to pass a tuple that contains the two arrays that you want to combine. Here is how you can use that method.

Let us create two arrays

import numpy as np

arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([6, 7, 8, 9, 10])
Enter fullscreen mode Exit fullscreen mode

Create the third variable that store the combination of arr1 and arr2. We combine the two arrays using join method

arr3 = np.concatenate((arr1, arr2))
Enter fullscreen mode Exit fullscreen mode

Print arr3 to view the output

print(arr3)
Enter fullscreen mode Exit fullscreen mode

The following is the output

[ 1  2  3  4  5  6  7  8  9 10]
Enter fullscreen mode Exit fullscreen mode

There you go, that is how you can combine two arrays in NumPy array. Thank you for reading and have a nice day!

Top comments (0)