Python concatenate arrays (Detailed Tutorial)

In this python tutorial, we will discuss the Python concatenate arrays and also we will cover these below topics:

  • How to concatenate arrays in python
  • Python concatenate arrays in list
  • Python concatenate arrays horizontally
  • Python concatenate array vertically
  • Python concatenate arrays to matrix
  • Python concatenate arrays in a loop
  • Python concatenate arrays of different dimensions
  • Python concatenate arrays numpy
  • Python concatenate arrays of strings
  • Python concatenate arrays by column
  • Python concatenate arrays of arrays
  • how to concatenate two 1d arrays in python
  • Concatenate arrays python without numpy
  • Concatenate two array python
  • Python concatenate multiple arrays
  • Python concatenate 3 arrays
  • Python concatenate arrays along an axis
  • 2-dimensional array python concatenate
  • Python concatenate two-byte arrays
  • Python concatenate two different size arrays
  • Python concatenate arrays of unequal length

How to concatenate arrays in python

We can use numpy.concatenate() to concatenate multiple numpy arrays.

Example:

import numpy as test

a = test.arange(5,9)
b = test.arange(2,4)
c= test.arange(6,8)
test.concatenate([a,b,c])

Oops, after executing the above code i got the below error

ModuleNotFoundError: No module named ‘numpy’

See the below output

ModuleNotFoundError: No module named 'numpy'

So to fix the above error you need to install ‘numpy’ with pip install numpy command.

To run the above command you need to open the command prompt(Run as administrator mode) and go to the path where python is installed and then run the above command.

See below

How to install numpay in windows 10

Now after installing numpy, you can able to run the above code which will concatenate both the arrays.

Python concatenate arrays in list

Here, we can see how to concatenate arrays in list in python.

  • In this example, I have taken two arrays as array1, and array2. To concatenate in the list, I have created a variable called list. The “+” operator is used to concatenate.
  • To convert the value into the string, I have used str() method.

Example:

array1 = [1,3,5,7] 
array2=  [2,4,6,8] 
list= array1 + array2
print ("list = " + str(list))

In the below screenshot, we can see the array is concatenated in the list.

Python concatenate arrays in list
Python concatenate arrays in list

Python concatenate arrays horizontally

Here, we can see concatenate arrays horizontally in python.

  • In this example, I have imported a module called numpy as np.
  • I have taken two arrays as Even_array and Odd_array, to pass the elements of the array np.array is used.
  • To concatenate the arrays horizontally, I have used np.hstack. The hstack is used to stack the array horizontal sequence.

Example:

import numpy as np
Even_array = np.array([2,4,6,8])
Odd_array = np.array([1,3,5,7])
Horizontal_array = np.hstack((Even_array, Odd_array))
print(Horizontal_array)

We can see the array in a horizontal format as the output. You can refer to the below screenshot for the output.

Python concatenate arrays horizontally
Python concatenate arrays horizontally

Python concatenate array vertically

  • In this example, I have imported a module called numpy as np.
  • I have taken two arrays as Even_array and Odd_array, and to pass the elements of the array np.array is used.
  • To concatenate the arrays vertically, I have used np.vstack. The vstack is used to stack the array sequence vertically.

Example:

import numpy as np
Even_array = np.array([2,4,6,8])
Odd_array = np.array([1,3,5,7])
Vertical_array = np.vstack((Even_array, Odd_array))
print(Vertical_array)

We can see the array in a vertical format as the output. You can refer to the below screenshot for the output.

Python concatenate array vertically
Python concatenate array vertically

Python concatenate arrays to matrix

Here, we can see concatenate arrays to matrix in python.

  • In this example, I have imported a module called numpy as np and taken two arrays as array1 and array2.
  • The np.array is used to pass the elements of the array.
  • To concatenate arrays np.concatenate is used, here the axis = 0, represents the rows so the array is concatenated below the row.
  • To get the output, I have used print(matrix).

Example:

import numpy as np 
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6],[7,8]])
matrix = np.concatenate((array1, array2), axis=0)
print(matrix)

The below screenshot shows the array in the matrix format as the output. You can refer to the below screenshot for the output.

Python concatenate arrays to matrix
Python concatenate arrays to matrix

Python concatenate arrays in a loop

Here, we can see concatenate arrays in a loop in python.

  • In this example, I have taken two arrays as fruits and chocolate. To concatenate the array for loop is used as for items in chocolate.
  • To append the items of chocolate into fruit, I have used fruits.append().
  • To get the output, I have used print(fruits).

Example:

fruits= ['mango','apple']
chocolate = ['dairymilk','milkybar']
for item in chocolate:
    fruits.append(item)
print(fruits)

Here, we can see that the two array are concantenated as the output. You can refer to the below screenshot for the output.

 Python concatenate arrays in a loop
Python concatenate arrays in a loop

Python concatenate arrays of different dimensions

Now, we can see how to concatenate arrays of different dimensions in python.

  • In this example, I have imported a module called numpy as np.
  • I have taken two arrays such as array1 and array2. The np.arange is used to create the array of the given range.
  • Here, np.arange(8) is the given range, and another array with range np.arange(2) is given.
  • To concatenate the array of two different dimensions. The np.column_stack((array1, array2)) is used.
  • To get the output, I have used print(array1).

Example:

import numpy as np
array1 = np.arange(8).reshape(2,4)
array2 = np.arange(2)
array1 = np.column_stack((array1, array2))
print(array1)

Here, the two different dimensions array are concatenated as the output. The below screenshot shows the output.

Python concatenate arrays of different dimensions
Python concatenate arrays of different dimensions

Python concatenate arrays numpy

Now, we can see concatenate arrays numpy in python.

  • In this example, I have imported a module called numpy as np. The Numpy is the library used to work with the array.
  • The np.array is used to pass the element of the array. To concatenate the array, I have used np.concatenate().
  • To get the output, I have used print(array).

Example:

import numpy as np
array1 = np.array([2,4,6])
array2 = np.array([1,3,5])
array = np.concatenate((array1, array2))
print(array)

The below screenshot shows the output.

Python concatenate arrays numpy
Python concatenate arrays numpy

Python concatenate arrays of strings

Here, we can see concatenate arrays of strings in python.

  • In this example, I have imported a module called numpy as np. I have taken two arrays as array1 and array2. The np.array is used to pass the items in the array.
  • As we are using string in the array np.char.add(array1, array2) is used.
  • To get the output, I have used print(string).

Example:

import numpy as np
array1 = np.array(['mobile', 'Laptop'])
array2 = np.array([' box', ' pen'])
string = np.char.add(array1, array2)
print(string)

We can see the concatenated array of string as the output. You can refer to the below screeenshot.

Python concatenate arrays of strings
Python concatenate arrays of strings

Python concatenate arrays by column

Here, we can see concatenate arrays by column in python.

  • In this example, I have imported a module called numpy as np. I have taken two arrays as array1 and array2 and to get the array concatenated in column format.
  • I have used np.coloumn_stack. The np.coloumn_stack takes the sequence of a 1-d array and stacks them in the column.
  • To get the output, I have used print(array).

Example:

import numpy as np
array1 = np.array((0, 1, 3))
array2 = np.array((2, 1, 4))
array = np.column_stack((array1,array2))
print(array)

The below screenshot shows the output as the concatenated array in the coloumn format.

Python concatenate arrays by column
Python concatenate arrays by column

Python concatenate arrays of arrays

Here, we can see concatenate arrays of arrays in python.

  • In this example, I have imported a module called numpy as np.
  • To create the array of the given ranges, I have used np.arange(5). Here, 5 is the given range
  • The print(a) is used to check the created array and np.concatenate (a, a) to concatenate the array of array, and axis=0 is taken here to concatenate the array row-wise.
  • To get the output print(b) is used.

Example:

import numpy as np
a=[np.arange(5),np.arange(5)]
print(a)
b=np.concatenate((a,a),axis=0)
print(b)

The concatenated array is seen in the output. You can refer to the below screenshot for the output.

Python concatenate arrays of arrays
Python concatenate arrays of arrays

How to concatenate two 1d arrays in python

Here, we can see how to concatenate two 1d arrays in python.

  • In this example, I have taken two arrays of one dimension such as array1 and array2.
  • To concantenate the array + operator is used as array = array1+array2.
  • To get the output, I have used print(array).

Example:

array1 = [3,6,9,12]
array2 = [4,8,12,16]
array = array1+array2
print(array)

We can see the concatenated string as the output. You can refer to the below screenshot for the output.

How to concatenate two 1d arrays in python
How to concatenate two 1d arrays in python

Concatenate arrays python without numpy

Here, we can see concatenate arrays python without numpy in python.

  • In this example, I have taken two arrays such as fruits and chocolate.
  • To concantenate the array ‘+‘ operator is used as array = fruits+chocolate.
  • To get the output, I have used print(array).

Example:

fruits= ['mango','apple']
chocolate = ['dairymilk','milkybar']
array = fruits+chocolate
print(array)

The below screenshot shows the concatenated array as the output. You can refer to the below screenshot for the output.

Concatenate arrays python without numpy
Concatenate arrays python without numpy

Concatenate two array python

Here, we can see concatenate two array in python.

  • In this example, I have taken two arrays as array1 and array2. To concatenate the array a for loop is used as for items in array2.
  • To append the items of array2 into array1, I have used array1.append(item).
  • To get the output, I have used print(array1).

Example:

array1 = [1,2,3,4]
array2 = [5,6,7,8]
for item in array2:
    array1.append(item)
print(array1)

The below screenshot shows the output of two concatenated arrays.

Concatenate two array python
Concatenate two array python

Python concatenate multiple arrays

Here, we can see how to concatenate multiple arrays in python

  • In this example, I have taken multiple arrays as array1, array2, array3, array4. To concatenate all the arrays “+” operator is used.
  • I have used print(array) to get the output.

Example:

array1 = [1,3,5,7] 
array2 = [2,4,6,8] 
array3 = [3,6,9,12]
array4 = [4,8,6,9]
array= array1 + array2 + array3 + array4
print(array)

The below screenshot shows the concatenated array as the output.

Python concatenate multiple arrays
Python concatenate multiple arrays

Python concatenate 3 arrays

Here, we can see how to concatenate 3 arrays in python

  • In this example, I have imported a module called numpy as np. I have three arrays as array1, array2, array3.
  • To concatenate the arrays, I have used np.concatenate(array1,array2,array3).
  • The axis=0 represents rows and axis=1 represents columns.
  • I have used print(array) to get the output.

Example:

import numpy as np
array1 = [1,3,5,7] 
array2 = [2,4,6,8] 
array3 = [3,6,9,12]
array= np.concatenate((array1,array2,array3), axis=0)
print(array)

The below screenshot shows the output.

Python concatenate 3 arrays
Python concatenate 3 arrays

This is how we can concatenate 3 arrays in Python.

Python concatenate arrays along an axis

Now, we can see how to concatenate arrays along an axis in python

  • In this example, I have imported a module called numpy as np. I have taken two arrays as array1, array2.
  • To concatenate arrays along an axis, I have taken np.stack((array1, array2), axis = 0) for axis0.
  • For axis1, I have used np.stack((array1, array2), axis = 1.
  • The axis=0 represents rows and axis=1 represents columns.

Example:

import numpy as np
array1 = np.array([ 1, 2, 3] ) 
array2 = np.array([ 4, 5, 6] ) 
axisarray1 = np.stack((array1, array2), axis = 0) 
print ("axis 0:\n ", axisarray1) 
axisarray2 = np.stack((array1, array2), axis = 1) 
print ("axis 1:\n ", axisarray2) 

The array is concatenated along the axis. The below screenshot shows the output.

Python concatenate arrays along an axis
Python concatenate arrays along an axis

2-dimensional array Python concatenate

Here, we can see how to concatenate 2-dimensional array in python.

  • In this example, I have imported a module called numpy as np.
  • To create the 2-d array, I have used np.arange(2,11).reshape(3,3).
  • To print the created array, I have used print(array1), print(array2).
  • The np.concatenate ((array1,array2),axis=1) is used to concatenate the array.
  • The axis=0 represents rows and axis=1 represents columns.

Example:

import numpy as np 
array1 = np.arange(2,11).reshape(3,3) 
array2 = np.arange(31,40).reshape(3,3) 
print(array1) 
print(array2) 
array = np.concatenate((array1,array2),axis=1) 
print(array)

The 2-d array is concatenated as the output. You can refer to the below screenshot for the output.

2-dimensional array python concatenate
2-dimensional array python concatenate

This is how to concatenate 2-dimensional array in Python.

Python concatenate two-byte arrays

Here, we can see how to concatenate two-byte arrays in python.

  • In this example, I have taken two-byte arrays as byt1, byt2.
  • To concatenate the two-byte arrays, I have used .join([bytearray(10), bytearray(5)]).
  • To get the output, I have used print(a).

Example:

byt1 = bytearray(10)
byt2 = bytearray(5)
a = b"".join([byt1, byt2])
print(a)

We can see that bytearray is concatenated as the output. The below screenshot shows the output.

Python concatenate two-byte arrays
Python concatenate two-byte arrays

This is how to concatenate two-byte arrays in Python.

Python concatenate two different size arrays

Here, we can see how to concatenate two different size arrays in python

  • In this example, I have taken arrays of three different sizes. I have used “+” to concatenate the array.
  • The print(array) is used to get the output.

Example:

array1 = [1] 
array2 = [2,4]
array3 = [3,6,9]
array = array1+array2+array3
print(array) 

You can refer to the below screenshot for the output.

Python concatenate two different size arrays
Python concatenate two different size arrays

This is how to concatenate two different size arrays in Python.

Python concatenate arrays of unequal length

Now, we can see how to concatenate arrays of unequal length in python.

  • In this example, I have imported a module called numpy as np, I have used np.zeros, np.ones to create the array of unequal lengths.
  • The np.append is used to concatenate arrays of unequal length.
  • The print(array) is used to get the output.

Example:

import numpy as np
array1 = np.zeros(8)
array2 = np.ones(2)
array = np.append(array1, array2)
print(array)

We can see that the zeros and ones are concatenated as the output. You can refer to the below screenshot for the output.

Python concatenate arrays of unequal length
Python concatenate arrays of unequal length

This is how we can concatenate arrays of unequal length in Python.

You may like the following Python tutorials:

In this tutorial, we have learned about Python concatenate arrays, and also we have covered these topics:

  • How to concatenate arrays in python
  • Python concatenate arrays in list
  • Python concatenate arrays horizontally
  • Python concatenate array vertically
  • Python concatenate arrays to matrix
  • Python concatenate arrays in a loop
  • Python concatenate arrays of different dimensions
  • Python concatenate arrays numpy
  • Python concatenate arrays of strings
  • Python concatenate arrays by column
  • Python concatenate arrays of arrays
  • how to concatenate two 1d arrays in python
  • Concatenate arrays python without numpy
  • Concatenate two array python
  • Python concatenate multiple arrays
  • Python concatenate 3 arrays
  • Python concatenate arrays along an axis
  • 2-dimensional array python concatenate
  • Python concatenate two-byte arrays
  • Python concatenate two different size arrays
  • Python concatenate arrays of unequal length