numpy.concatenate() function in Python

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The numpy.concatenate() function combines multiple arrays into a single array along a specified axis. This function is particularly useful when working with large datasets or performing operations that require merging data from different sources. Unlike other array-joining functions like numpy.vstack() , numpy.hstack() which only join arrays vertically or horizontally numpy.concatenate() provides greater flexibility by allowing you to specify the axis along which the arrays are joined.

Syntax of numpy.concatenate()

The syntax for the numpy.concatenate() function is as follows:

numpy.concatenate((array1, array2, ...), axis=0, out=None, dtype=None)

Parameters:

  • arrays: A sequence of input arrays to be concatenated. These arrays must have the same shape along all axes except the one specified by axis.
  • axis: The axis along which the arrays will be joined. Default is 0 (the first axis).
  • out: If provided the result will be placed in this array.
  • dtype: It overrides the data type of the output array.

Key Features of numpy.concatenate()

  1. Flexible Axis Specification : You can concatenate arrays along any axis make it versatile for multi-dimensional data.
  2. Data Compatibility : The arrays must have matching shapes along all axes except the one being concatenated.
  3. Efficient Memory Usage : The function creates a new array but shares the underlying data from the input arrays when possible.
  4. Supports Higher Dimensions : Works easily with 1D, 2D and higher-dimensional arrays.

Examples of Using numpy.concatenate()

Let us now look at some practical examples to understand how numpy.concatenate() works

Example 1: Concatenating 1D Arrays

Suppose you have two 1D arrays and want to combine them into a single array.

Python
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

result = np.concatenate((arr1, arr2))

print("Result:", result)

Output :

Result: [1 2 3 4 5 6]

Example 2: Concatenating 2D Arrays Along Rows (axis=0)

For 2D arrays you can concatenate along rows (default behavior) or columns

Python
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])

result = np.concatenate((arr1, arr2), axis=0)

print("Result:\n", result)

Output :

Numpy_concatenate
numpy_concatenate

Comparison with Other Functions

While numpy.concatenate() is versatile there are other functions in NumPy that perform similar tasks:

  1. numpy.vstack() :
    • Stacks arrays vertically along rows equivalent to axis=0.
    • Limited to stacking along the first axis.
  2. numpy.hstack():
    • Stacks arrays horizontally along columns equivalent to axis=0.
    • Limited to stacking along the second axis.
  3. numpy.append():
    • Appends values to an array along a specified axis.
    • Less efficient than because it creates a copy of the original array.

For scenarios which require flexibility and efficiency numpy.concatenate() is the preferred choice.


Similar Reads