# Python Program to Concatenate Two Arrays

## What is Concatenation of Arrays?

The process of combining the arrays into a single array or merging the arrays into a single array is known as Concatenation of arrays. This mechanism can be done in many ways using several techniques. Let us discuss all techniques that help in concatenation of arrays in Python.

### Input Output Scenarios

Consider three arrays to perform concatenation.

arr1 = [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ Python ”]
arr2 = [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ C ”]
arr3 = [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ Java ”]


Now, the concatenated array in

• Horizontal order will be [" Hello ", " World ", " My ", " Name ", " is ", " Python ", " Hello ", " World ", " My ", " Name ", " is ", " C ", " Hello ", " World ", " My ", " Name ", " is ", " Java "].

• The horizontal concatenation merges all the elements of the arrays altogether without separation, horizontally.

• Vertical order will be [ [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ Python ”], [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ C ”], [“ Hello ”, “ World ”, “ My ”, “ Name ”, “ is ”, “ Java ”]].

• The vertical concatenation merges the elements according to the array they are present in and the separation of the arrays are clearly mentioned within this type of concatenation.

### Concatenation of Arrays

There exists various approaches to array concatenation, each of which can result in either a vertically or horizontally concatenated array, or a matrix. Let us explore these techniques one by one.

## Using Concatenation() Method of Numpy Module

The method concatenate() is one of the built-in methods of Numpy module. It is used in order to concatenate the given arrays. This method cannot be accessed directly unless the numpy module is imported and an object is created under the numpy module.

By default, the concatenation of the arrays followed by using this method results with a Horizontally Concatenated array.

### Syntax

The following is the syntax for using the “ concatenate() ” method.

concatenated_array = n.concatenate([narr1, narr2, narr3, narr4,. . . . narrN])


The arrays arr1, arr2, arr3, arr4, . . . . . , arrN are supposed to be concatened. In order to perform that operation, they must be converted into numpy arrays, which are named as narr1, narr2, narr3, . . . . , narrN respectively.

### Example

In the following example, we are going to combine or concatenate multiple arrays using the method concatenate() of the numpy module.

import numpy as n
arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
arr3 = [9, 10, 11, 12]

narr1 = n.array(arr1)
narr2 = n.array(arr2)
narr3 = n.array(arr3)

concatenated_array = n.concatenate([narr1, narr2, narr3])

print(concatenated_array)


### Output

The output of the above program is as follows −

[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12]


## Using + Operator

The operator “+” can be used to combine and merge the arrays which incorporates Concatenation very easily. Let us have a look at the syntax and an example program using the “ + ” operator.

### Syntax

The following is the syntax for using the “ + ” operator −

concatenated_array = arr1 + arr2 + arr3 + arr4 + . . . . arrN


Here, the arrays arr1, arr2, arr3, arr4, . . . . . , arrN are considered in order be concatened by using + operator.

### Example

In the following example, we are going to combine or concatenate multiple arrays using the operator “+”.

arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
arr3 = [9, 10, 11, 12]

concatenated_array = arr1 + arr2 + arr3
print(concatenated_array)


### Output

The output of the above program is as follows &miinus;

[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12]


## Using Vstack() Method of Numpy Module in Python

The inbuilt method present within the Numpy module in order to perform concatenation is vstack(). This method is used to concatenate the arrays. The result of the program after using this method will produce a vertically concatenated array which cannot be done by any other techniques discussed before. This cannot be accessed directly unless the numpy module is imported and an object is created under the numpy module.

### Syntax

The following is the syntax for using the “ concatenate() ” method −

concatenated_array = n.vstack((narr1, narr2, narr3, narr4,. . . . ., narrN))


The arrays narr1, narr2, narr3, . . . . . . , narrN are the numpy arrays which must be concatenated.

### Example

In the following example, we are going to combine or concatenate multiple arrays using the method vstack().

import numpy as n
arr1 = [1, 2, 3, 4]
arr2 = [5, 6, 7, 8]
arr3 = [9, 10, 11, 12]

narr1 = n.array(arr1)
narr2 = n.array(arr2)
narr3 = n.array(arr3)

concatenated_array = n.vstack((narr1, narr2, narr3))
print(concatenated_array)


### Output

The output of the above program is as follows −

[[ 1  2  3  4]
[ 5  6  7  8]
[ 9 10 11 12]]


## Conclusion

We can conclude that there are several techniques that help in Concatenation of arrays. But, depending on the type of the concatenation we require, we need to choose the technique that must be applied. This is how the mechanism of Concatenation of arrays work.

Updated on: 05-May-2023

2K+ Views 