In this post, we will learn how to concatenate Array in Python. Python does not have an array as a built-in datatype instead the lists are used as an array in Python also known as a dynamic array. We often confuse Python list and array. The array is sequence type and the object stored in them are constraints.
1.numpy.conatenate() function to concatenate Array in Python
The NumPy library array concatenates() function concatenates two numpy arrays of the same shape either row-wise or column-wise.This function by default concatenate arrays row-wise (axis=0).
In this example, we are concatenating two or multiple numpy arrays using concatenate function. To use this function we need to import the NumPy library using “import numpy as np”
import numpy as np
nparr1 = np.array([13, 14, 15, 18, 20])
nparr2 = np.array([22, 32, 33, 34, 36])
nparr3 = np.array([5, 10, 12, 13, 14])
nparr4 = np.array([2, 4, 6, 8, 10])
joined_nparr = np.concatenate( (nparr1, nparr2) )
print('concatenate two arrays:',joined_nparr)
muti_nparr = np.concatenate( (nparr1, nparr2) )
print('concatenate mutiple arrays:',muti_nparr)
Output
concatenate two arrays: [13 14 15 18 20 22 32 33 34 36]
concatenate mutiple arrays: [13 14 15 18 20 22 32 33 34 36]
2. How to concatenate Array in Python using for loop
In this example, we are using for loop to iterate over arr1.On each iteration append the element at end of arr2 by using the append() method.
arr1 = [22, 32, 33, 34, 36]
arr2 = [5, 10, 12, 13, 14]
for ele in arr1:
arr2.append(ele)
print(arr2)
Output
[5, 10, 12, 13, 14, 22, 32, 33, 34, 36]
3. Concatenate array Using + operator
The + operator is used to concatenate a string and we can use it to concatenate two or multiple arrays. We have concatenated animal_arr, and animal_arr2 using the plus operator.
animal_arr = ['cat','bee','cat','cat','rat']
animal_arr2 = [1,2,3,'mouse']
cont_arr = animal_arr +animal_arr2
print('Two arrays:',cont_arr)
number_arr = [48,67,63]
muti_arra = animal_arr +animal_arr2 +number_arr
print('Mutiarrs:',muti_arra )
Output
Two arrays: ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']
Mutiarrs: ['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse', 48, 67, 63]
4.List.append() to concatenate array
We can use a list. append() method of list class appends an array at end of the first array. It appends the whole array at the end of the original array.It does not append a single element but appends a whole array. As we can see the whole array animal_arr2 is appended at end of animal_arr.
animal_arr = ['cat','bee','cat','cat','rat']
animal_arr2 = [1,2,3,'mouse']
animal_arr.append(animal_arr2 )
print(animal_arr)
Output
['cat', 'bee', 'cat', 'cat', 'rat', [1, 2, 3, 'mouse']]
5. How to concatenate array in python Using list comprehension
In this example, We have used list comprehension to concatenate two arrays in python. Let us understand with an example.
animal_arr = ['cat','bee','cat','cat','rat']
animal_arr2 = [1,2,3,'mouse']
concat_Arr = [item_y for item_x in [animal_arr, animal_arr2] for item_y in item_x]
print(concat_Arr)
Output
['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse']
6. How to concatenate array Using *(asterisk)
This unpacking *operator is used in Python 3.6 and above, with iterable(list, string, tuple, dictionary) operand, we have used concatenated multiple arrays using the *operator.
animal_arr = ['cat','bee','cat','cat','rat']
animal_arr2 = [1,2,3,'mouse']
number_arr = [48,67,63]
concat_Arr = [*animal_arr, *animal_arr2,*number_arr ]
print(concat_Arr)
Output
['cat', 'bee', 'cat', 'cat', 'rat', 1, 2, 3, 'mouse', 48, 67, 63]
7. How to concatenate array in Python Horizontally
In this example, We have used the Numpy library hstack() method to concatenate two arrays animal_arr, and animal_arr2 by passing them as parameters to np. hstack() method.
import numpy as np
animal_arr = np.array(['cat','bee','cat','cat','rat'])
animal_arr2 = np.array([1,2,3,'mouse'])
hor_Arr = np.hstack((animal_arr, animal_arr2))
print(hor_Arr)
Output
['cat' 'bee' 'cat' 'cat' 'rat' '1' '2' '3' 'mouse']
8. How to concatenate array in Python vertically
In this example, We have used the Numpy library vstack() method to concatenate two arrays animal_arr,animal_arr2 by passing them as parameter np.vstack() method.
import numpy as np
animal_arr = np.array(['cat','bee','cat','rat'])
animal_arr2 = np.array([1,2,3,'mouse'])
Verical_Arr = np.vstack((animal_arr, animal_arr2))
print(Verical_Arr)
Output
[['cat' 'bee' 'cat' 'rat']
['1' '2' '3' 'mouse']]