concatenate() (NumPy)

np.concatenate() joins a sequence of arrays end to end along an existing axis. It is the general-purpose tool for combining arrays, with hstack, vstack, and stack as convenient specializations.

NumPy

Pass a list or tuple of arrays and the axis to join along. With axis=0 it stacks them vertically (more rows); with axis=1 it joins them horizontally (more columns). The arrays must match in shape on every axis except the one you are joining.

Because it works along an existing axis, concatenate never adds a new dimension. When you want to stack arrays into a brand-new axis instead — turning several 1-D arrays into a 2-D array — use np.stack. hstack and vstack are friendly wrappers for the common horizontal and vertical cases.

As with all NumPy joins, the result is a fresh copy in new memory; arrays are fixed-size, so there is no in-place append.

Syntax

np.concatenate([a, b], axis=0)

Examples

Python

import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
print(np.concatenate([a, b]))

Output:

[1 2 3 4]

Learn it in a lesson

See also

Back to the Dictionary