This post was last Updated on June 14, 2022 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.
In this article, we will discuss how to concatenate arrays in Python. An array is a data structure that will store the same data type elements. In Python, we can create an array using the NumPy module.
NumPy stands for numeric Python used to process the arrays. We can create an array in numpy and reverse the array using some inbuilt functions available in NumPy.
Also Read: How to Create an Empty Array In Python
Syntax:
import numpy
Creation:
We can create an array through the NumPy module.
Syntax:
numpy.array([elements])
We can also consider a list array in Python.
How to Concatenate Arrays in Python
Python list is a data structure that stores elements with multiple data types. We can create a list by using [].
Let’s see different scenarios to concatenate arrays in Python.
Also Read: How to Reverse an Array In Python [Flip Array]
Method 1: Concatenate numpy arrays using concatenate()
In this case, we will concatenate two/more numpy arrays using concatenate() method.
Syntax:
numpy.concatenate([array1,array2,.....,array n])
where, array1, array2,.,array n are the arrays to be concatenated.
Example 1:
In this example, we will concatenate two arrays with integer elements
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #concatenate two arrays print(numpy.concatenate([array1,array2]))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] After concatenating [ 23 45 67 879 76 5 56 67 65 43]
Example 2:
In this example, we will concatenate three arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,2,3,4,5]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #concatenate two arrays print(numpy.concatenate([array1,array2,array3]))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 2 3 4 5] After concatenating [ 23 45 67 879 76 5 56 67 65 43 1 2 3 4 5]
Also Read: Python 3 Program To Find Largest Element In An Array or List
Method 2: Concatenate lists using the + operator
In this case, we will concatenate two/more lists using the + operator
Syntax:
array1+array2+.......+array n
Where, array1, array2,.,array n are the arrays to be concatenated.
Example 1:
In this example, we will concatenate two arrays with integer elements.
#create first array array1=[23,45,67,879,76] #create second array array2=[5,56,67,65,43] #display print(array1) #display print(array2) print("After concatenating") #concatenate two arrays print(array1+array2)
Output:
[23, 45, 67, 879, 76] [5, 56, 67, 65, 43] After concatenating [23, 45, 67, 879, 76, 5, 56, 67, 65, 43]
Example 2:
In this example, we will concatenate three arrays with integer elements.
#create first array array1=[23,45,67,879,76] #create second array array2=[5,56,67,65,43] #create third array array3=[1,2,3,4,5] #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #concatenate two arrays print(array1+array2+array3)
Output:
[23, 45, 67, 879, 76] [5, 56, 67, 65, 43] [1, 2, 3, 4, 5] After concatenating [23, 45, 67, 879, 76, 5, 56, 67, 65, 43, 1, 2, 3, 4, 5]
Also Read: What Is Python Used For? Applications of Python
Method 3: Concatenate NumPy arrays horizontally
We will concatenate two/more numpy arrays horizontally using the hstack() method.
Syntax:
numpy.hstack((array1,array2,.....,array n))
Where, array1, array2,.,array n are the arrays to be concatenated.
Example 1:
In this example, we will concatenate two arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #concatenate arrays print(numpy.hstack((array1,array2)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] After concatenating [ 23 45 67 879 76 5 56 67 65 43]
Example 2:
In this example, we will concatenate three arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,2,3,4,5]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #concatenate arrays print(numpy.hstack((array1,array2,array3)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 2 3 4 5] After concatenating [ 23 45 67 879 76 5 56 67 65 43 1 2 3 4 5]
Also Read: How To Automate Google Search With Python
Method 4: Concatenate NumPy arrays vertically
We will vertically concatenate two/more numpy arrays using the vstack() method.
Syntax:
numpy.vstack((array1,array2,.....,array n))
Where, array1, array2,.,array n are the arrays to be concatenated.
Example 1:
In this example, we will concatenate two arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #concatenate arrays print(numpy.vstack((array1,array2)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] After concatenating [[ 23 45 67 879 76] [ 5 56 67 65 43]]
Example 2:
In this example, we will concatenate three arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,2,3,4,5]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #concatenate arrays print(numpy.vstack((array1,array2,array3)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 2 3 4 5] After concatenating [[ 23 45 67 879 76] [ 5 56 67 65 43] [ 1 2 3 4 5]]
Also Read: 35 Funny And Best Python Programming Memes
Method 5: Concatenate numpy arrays by column
In this case, we will concatenate two/more numpy arrays by column using the column_stack() method.
Syntax:
numpy.column_stack((array1,array2,.....,array n))
Where, array1, array2,.,array n are the arrays to be concatenated.
Example 1:
In this example, we will concatenate two arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #concatenate arrays print(numpy.column_stack((array1,array2)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] After concatenating [[ 23 5] [ 45 56] [ 67 67] [879 65] [ 76 43]]
Also Read: How to Convert Binary to Decimal in Python [5 Methods]
Example 2:
In this example, we will concatenate three arrays with integer elements.
#importing the numpy module import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,2,3,4,5]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #concatenate arrays print(numpy.column_stack((array1,array2,array3)))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 2 3 4 5] After concatenating [[ 23 5 1] [ 45 56 2] [ 67 67 3] [879 65 4] [ 76 43 5]]
Also Read: How To Create Keylogger In Python
Method 6: Concatenate arrays in a loop
Here we are using a for loop to iterate over the second array, and by using the append() function, we can add the second array to the first array.
Example:
In this example, we will concatenate two arrays using the append() function through for loop.
#create first array array1=[23,45,67,879,76] #create second array array2=[5,56,67,65,43] #display print(array1) #display print(array2) #use for loop to apped second array to first array for i in array2: array1.append(i) print("After concatenating") #display the concatenated array print(array1)
Output:
[23, 45, 67, 879, 76] [5, 56, 67, 65, 43] After concatenating [23, 45, 67, 879, 76, 5, 56, 67, 65, 43]
Also Read: 20 Best Python Development Companies [2022]
Method 7: Concatenate arrays of strings
Here we will concatenate the array of strings by using numpy.char() method. And, add() is used to add the two string arrays.
Syntax:
numpy.char.add(array1,array2)
Where array1 is the first array, and array2 is the second array.
Example:
In this example, we will concatenate two string arrays.
#importing the numpy module import numpy #create first array array1=numpy.array(["PHP","R"]) #create second array array2=numpy.array(["java","jsp"]) #display print(array1) #display print(array2) print("After concatenating") #concatenate arrays print(numpy.char.add(array1,array2))
Output:
['PHP' 'R'] ['java' 'jsp'] After concatenating ['PHPjava' 'Rjsp']
Also Read: How To Use Python For Browser Games Development?
Method 8: concatenate arrays along the axis
Here we will concatenate the array of strings by using numpy.stack() function and concatenate along the row axis and column axis. The row axis is represented by 0, and the column axis is represented by 1.
Syntax:
numpy.stack((array1,array2,..array n),axis)
Example 1:
In this example, we will concatenate two integer arrays by row axis.
import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #display the concatenated array print(numpy.stack((array1,array2),axis=0))
Output:
[23, 45, 67, 879, 76] [5, 56, 67, 65, 43] After concatenating [[ 23 45 67 879 76] [ 5 56 67 65 43]]
Also Read: How To Display A Calendar In Python
Example 2:
In this example, we will concatenate two integer arrays by column axis.
import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #display print(array1) #display print(array2) print("After concatenating") #display the concatenated array print(numpy.stack((array1,array2),axis=1))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] After concatenating [[ 23 5] [ 45 56] [ 67 67] [879 65] [ 76 43]]
Also Read: 7 Best Python IDE for Windows [Code Editors]
Example 3:
In this example, we will concatenate three integer arrays by column axis.
import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,1,0,1,1]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #display the concatenated array print(numpy.stack((array1,array2,array3),axis=1))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 1 0 1 1] After concatenating [[ 23 5 1] [ 45 56 1] [ 67 67 0] [879 65 1] [ 76 43 1]]
Also Read: Python 3 Program To Find The Sum of An Array
Example 4:
In this example, we will concatenate three integer arrays by row axis.
import numpy #create first array array1=numpy.array([23,45,67,879,76]) #create second array array2=numpy.array([5,56,67,65,43]) #create third array array3=numpy.array([1,1,0,1,1]) #display print(array1) #display print(array2) #display print(array3) print("After concatenating") #display the concatenated array print(numpy.stack((array1,array2,array3),axis=0))
Output:
[ 23 45 67 879 76] [ 5 56 67 65 43] [1 1 0 1 1] After concatenating [[ 23 45 67 879 76] [ 5 56 67 65 43] [ 1 1 0 1 1]]
Also Read: How to Check If Dict Has Key in Python [5 Methods]
Method 9: Concatenate two different size arrays
We can concatenate different size arrays by using the + operator.
Example 1:
In this example, we will create two arrays of different sizes and concatenate them using the + operator.
#create first array array1=[23] #create second array array2=[5,56,67,65,43] #display print(array1) #display print(array2) print("After concatenating") #display the concatenated array print(array1+array2)
Output:
[23] [5, 56, 67, 65, 43] After concatenating [23, 5, 56, 67, 65, 43]
Also Read: 10 Best Python Libraries for Image Processing
Example 2:
In this example, we will create five arrays of different sizes and concatenate them using the + operator.
#create first array array1=[23] #create second array array2=[5,56,67,65,43] #create third array array3=[0,1,2,3,4,5,6,7,8,8,67] #create forth array array4=[5,56] #create fifth array array5=[5,56,67,65] #display print(array1) #display print(array2) #display print(array3) #display print(array4) #display print(array5) print("After concatenating") #display the concatenated array print(array1+array2+array3+array4+array5)
Output:
[23] [5, 56, 67, 65, 43] [0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 67] [5, 56] [5, 56, 67, 65] After concatenating [23, 5, 56, 67, 65, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 67, 5, 56, 5, 56, 67, 65]
Also Read: How to Create Python Empty Set
Method 10: concatenate two arrays with extend()
You can use the extend() Python function to add the second array to the first array.
Syntax:
array1.extend(array2)
Example:
In this example, we will concatenate the two arrays using extend() function.
#create first array array1=[23] #create second array array2=[5,56,67,65,43] #display print(array1) #display print(array2) print("After concatenating") array1.extend(array2) #display the concatenated array print(array1)
Output:
[23] [5, 56, 67, 65, 43] After concatenating [23, 5, 56, 67, 65, 43]
Also Read: How To Make A Digital Clock In Python Using Tkinter
Summary
This article discussed ten scenarios to concatenate two or more arrays in Python. We discussed how to concatenate row-wise and column-wise. We also discussed concatenating arrays horizontally and vertically using hstack() and vstack() methods that are available in the NumPy module.