Concatenation of Array in Python [7+ Examples]

In this Python blog, I will explain various methods and ways for concatenation of array in Python, I will explain each method with the help of some illustrative examples. I will also explain how to concatenate arrays in Python without NumPy functions and how to concatenate arrays of different sizes Python.

To concatenate arrays in Python we can use concatenate(), stack(), hstack(), vstack(), column_stack(), char.add(), and append() functions from the NumPy module. We can even create arrays using the array module in Python and then concatenate them without numpy functions. Arrays in Python can be of different sizes, we can use concatenate(), append(), and column_stack() functions to concatenate them.

Methods for concatenation of array in Python

There are many different methods in Python for the concatenation of an array:

  1. concatenate()
  2. numpy.stack()
  3. numpy.hstack()
  4. numpy.vstack()
  5. numpy.column_stack()
  6. numpy.char.add()
  7. numpy.append()

Let’s see them one by one using some illustrative examples:

Method 1: Python concatenate arrays using concatenate() function

The NumPy concatenate() is a general-purpose Python method to concatenate two or more numpy arrays in Python along a specified axis. By default, it concatenates along the first dimension (axis 0). The input Python arrays must have the same shape, except in the dimension corresponding to the axis.

Scenario: Consider a situation where we have to combine the data stored as numpy arrays in Python.

import numpy as np
temperatures_ny = np.array([32, 30, 35, 40, 45])
temperatures_la = np.array([70, 72, 68, 75, 74])
combined_temperatures = np.concatenate((temperatures_ny, temperatures_la))
print(combined_temperatures)

Output: There are two separate arrays in Python containing data. By using np.concatenate() function in Python, these two numpy arrays are merged into one continuous Python numpy array.

[32 30 35 40 45 70 72 68 75 74]
concatenation of array in python

The concatenate() function from the numpy library is the most effective way of concatenation of array in Python.

Method 2: Python array concatenate using the numpy.stack() function

The numpy stack() method joins two or more Python arrays along a new axis. It is useful when you have numpy arrays in Python of the same shape and you want to join them in such a way that the result has one more dimension. For example, if you stack two 1D arrays, the result will be a 2D array with the input arrays as its rows.

For instance: In this, we are stacking the data of two arrays through Python for better comparison.

import numpy as np
revenue_startup_1 = np.array([50000, 55000, 52000, 58000, 60000])
revenue_startup_2 = np.array([48000, 53000, 51000, 56000, 59000])
stacked_revenue = np.stack((revenue_startup_1, revenue_startup_2), axis=0)
print(stacked_revenue)

Output: Using np.stack(), we are stacking the two arrays in Python along a new axis (axis 0 in this case), creating a 2D numpy array. Each row in this 2D array corresponds to one of the data of an array, providing an easy way to compare them side-by-side.

[[50000 55000 52000 58000 60000]
 [48000 53000 51000 56000 59000]]
array concatenation in python

This way we can use the stack() function for the concatenation of array in Python.

Method 3: Concatenate two arrays Python using numpy.hstack() function

The Python hstack() numpy function stands for the horizontal stacking. It stacks NumPy arrays in sequence horizontally (i.e., column-wise), increasing the number of columns. If you have two 1D arrays in Python, this function will concatenate them to produce another 1D numpy array.

Example: Combining the data of two arrays in Python with the help of the hstack() NumPy function.

import numpy as np
population_texas = np.array([2000000, 2500000, 3000000, 3500000, 4000000])
population_california = np.array([2200000, 2700000, 3200000, 3700000, 4200000])
combined_population = np.hstack((population_texas, population_california))
print(combined_population)

Output: The np.hstack() is used to horizontally stack these two numpy arrays in Python, resulting in a single numpy array that holds the data for both side-by-side.

[2000000 2500000 3000000 3500000 4000000 2200000 2700000 3200000 3700000
 4200000]
python array concat

This way we can use the hstack() function from the numpy library for the concatenation of array in Python.

Method 4: Concatenate array in Python using the numpy.vstack()

The NumPy vstack() method stands for vertical stacking. It stacks arrays in sequence vertically (i.e., row-wise). It is useful for appending rows to a numpy 2D array in Python. i.e., If you have two 1D arrays, this function will produce a 2D array where each original Python array is a row.

Example: Concatenating arrays in Python with the help of vstack() numpy function.

import numpy as np
gdp_2021 = np.array([20.2, 20.4, 20.8, 21.1])
gdp_2022 = np.array([21.5, 21.7, 21.9, 22.1])
gdp_2023 = np.array([22.3, 22.5, 22.7, 22.9])
gdp_data = np.vstack((gdp_2021, gdp_2022, gdp_2023))
print(gdp_data)

Output: Using np.vstack in Python, we are stacking three arrays vertically, resulting in a 2D array, where each row represents an array and the columns represent the data.

[[20.2 20.4 20.8 21.1]
 [21.5 21.7 21.9 22.1]
 [22.3 22.5 22.7 22.9]]
python array concatenation

This way we can use the NumPy vstack() function for the concatenation of array in Python.

Method 5: How to concatenate two arrays in Python using numpy.column_stack()

The NumPy column_stack() method stacks 1D arrays as columns of a 2D array. Useful when you have several 1D arrays that you want to stack together as columns of a 2D array. It is similar to hstack() but treats 1D arrays as columns rather than elements of a 1D output Python array.

For instance: In this example, there are two numpy arrays containing two different data. and we have to concatenate them through Python.

import numpy as np
survey_1 = np.array([100, 200, 150])
survey_2 = np.array([80, 220, 130])
combined_survey_data = np.column_stack((survey_1, survey_2))
print(combined_survey_data)

Output: Using the np.column_stack() function in Python, we are stacking these two arrays as columns in a 2D array.

[[100  80]
 [200 220]
 [150 130]]
concatenate arrays python

The NumPy column_stack() function is used for the concatenation of array in Python.

Method 6: Python concatenate array using numpy.char.add()

The numpy.char.add() function in NumPy Python is used for element-wise string concatenation. This means that for two numpy arrays of strings, it concatenates corresponding pairs of strings from the two arrays in Python.

Example: Let’s learn how Python concatenates arrays of strings using some NumPy function.

import numpy as np
cities = np.array(["New York", "Los Angeles", "Chicago"])
states = np.array(["-NY", "-CA", "-IL"])
full_locations = np.char.add(cities, states)
print(full_locations)

Output: Here, we have two numpy arrays of strings. The np.char.add function is used for element-wise string concatenation of these two arrays in Python.

['New York-NY' 'Los Angeles-CA' 'Chicago-IL']
python concatenate array of strings

This way we can use numpy.char.add() function for the concatenation of array in Python with strings.

Method 7: How to concatenate array in Python using numpy.append() function

The Numpy append() function appends the values in one array to the end of another array in Python. It is useful for adding the elements of one numpy array to the end of another numpy array.

Example: Let’s concatenate two numpy arrays using the numpy append() function in Python.

import numpy as np
revenue_ny = np.array([5.2, 4.8, 6.1, 5.5])
revenue_ca = np.array([6.5, 6.6, 7.2, 6.8])
total_revenue = np.append(revenue_ny, revenue_ca)
print("Quarterly Revenue Data (NY + CA):", total_revenue)

Output: The np.append() function is used to combine the two NumPy arrays into a single array, containing the data of both arrays in Python.

Quarterly Revenue Data (NY + CA): [5.2 4.8 6.1 5.5 6.5 6.6 7.2 6.8]
concat array in python

This way we can use the append() function from the NumPy library for the concatenation of array in Python.

These were all the methods in the NumPy library that can be used for the concatenation of array in Python.

Concatenate arrays Python without NumPy

Python’s standard library has an array module, which provides an array data structure. This structure is more space-efficient than Python lists for storing numerical data. The array module also includes methods for performing operations on arrays, including concatenation.

Example: Below is an example of how to concatenate two arrays using the array module:

import array
array1 = array.array('i', [1, 2, 3, 4, 5])
array2 = array.array('i', [6, 7, 8, 9, 10])
concatenated_list = array1.tolist() + array2.tolist()
concatenated_array = array.array('i', concatenated_list)
print("Concatenated Array:", concatenated_array)

Output: To concatenate arrays without numpy, we first convert them to Python lists using the tolist() method. This is necessary because the array module does not provide a direct method for array concatenation in Python. After converting to lists in Python, we can use the + operator to concatenate them.

Since our goal is to obtain a concatenated array in Python, we need to convert our result back into an array in Python. We do this by passing the type code ‘i’ and the concatenated_list to the array constructor.

Concatenated Array: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
add two arrays python without numpy

This way we can use the array module in Python for concatenation of array in Python without NumPy.

Concatenate arrays of different sizes Python

NumPy arrays of different sizes in Python refer to sequences or collections of elements where the number of elements or the dimensions of the arrays differ from one another.

Concatenating arrays of different sizes is a common task in data manipulation and scientific computing. Python, with its powerful libraries such as NumPy, provides multiple functions to seamlessly perform these operations.

Method 1: Concatenation of array in Python of different sizes using concatenate()

Example: Imagine a case where you have two different Numpy arrays in Python and you have to concatenate them.

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

Output: The concatenate() function in NumPy joins a sequence of arrays along an existing axis in Python.

[[1 2]
 [3 4]
 [5 6]]
array concatenation python

Method 2: Python concatenate two different size arrays using numpy.append()

Example: Take two Numpy arrays of size and concatenate them through Python.

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

Output: The append() function in NumPy adds values at the end of an array in Python.

[[1 2]
 [3 4]
 [5 6]]
concatenate python array

Method 3: Python concatenate arrays of different dimensions using column_stack()

Example: Let’s take the different arrays of different dimensions in Python and then try to concatenate them.

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

Output: The column_stack() function in the numpy module stacks 1D arrays as columns of a 2D array in Python.

[[0 1 2 3 0]
 [4 5 6 7 1]]
how to concatenate two arrays python

Conclusion

We have seen seven different methods for the concatenation of array in Python like concatenate(), stack(), hstack(), vstack(), column_stack(), char.add(), and append() function from the numpy module with examples.

I have also explained how one can concatenate arrays in Python without the numpy function and how we can concatenate arrays of different sizes in Python with some illustrative examples.

Knowing the advantages and disadvantages of all the methods, the choice of selection of the method depends upon one’s problem or requirement.

You may like the following Python tutorials: