NumPy How to Concatenate Two Arrays

To concatenate two arrays with NumPy:

  1. Import numpy.
  2. Put two arrays in a list.
  3. Call numpy.concatenate() on the list of arrays.

For instance:

import numpy as np

A = np.array([1, 2])
B = np.array([3, 4])

C = np.concatenate([A, B])

print(C)

Output:

[1 2 3 4]

This is a quick answer.

To learn more ways to concatenate arrays and about their efficiency, please, stick around.

4 Ways to Concatenate 1D NumPy Arrays

There are four built-in ways to concatenate arrays in NumPy.

Before introducing these, it is important you understand that all these approaches use the numpy.concatenate() under the hood.

You probably are going to use one of these four. But it is still worth understanding that other options exist. Furthermore, it is insightful to see how these perform against one another.

1. numpy.r_

The numpy.r_ concatenates slice objects along the first axis. It offers you to build up arrays quickly.

One way to use r_ is to concatenate two 1D arrays.

For instance:

import numpy as np

A = np.array([1, 2])
B = np.array([3, 4])

C = np.r_[A, B]

print(C)

Output:

[1 2 3 4]

2. numpy.stack.reshape

The numpy.stack() function joins a collection of arrays along a new axis.

When you have joined two arrays using stack() you can call the reshape(-1) function to flatten the array of arrays.

For instance:

import numpy as np

A = np.array([1, 2])
B = np.array([3, 4])

C = np.stack([A, B]).reshape(-1)

print(C)

Output:

[1 2 3 4]

3. numpy.hstack

The numpy.hstack() function stacks a sequence column-wise. In other words, the function concatenates the arrays:

  • Along the second axis in general.
  • Along the first axis on 1D arrays.

Thus, you can use this function to concatenate two arrays.

For instance:

import numpy as np

A = np.array([1, 2])
B = np.array([3, 4])

C = np.hstack([A, B])

print(C)

Output:

[1 2 3 4]

4. numpy.concatenate

The numpy.concatenate() function merges two arrays together, forming a new array with all the elements from the original arrays.

For instance:

import numpy as np

A = np.array([1, 2])
B = np.array([3, 4])

C = np.concatenate([A, B])

print(C)

Output:

[1 2 3 4]

Performance Comparison

Let’s see how each of the concatenation approaches perform against one another.

import numpy as np
import perfplot

perfplot.show(
    setup=lambda n: np.random.rand(n),
    kernels=[
        lambda A: np.r_[A, A],
        lambda A: np.stack([A, A]).reshape(-1),
        lambda A: np.hstack([A, A]),
        lambda A: np.concatenate([A, A]),
    ],
    labels=["np.r_", "np.stack.reshape", "np.hstack", "np.concatenate"],
    n_range=[2 ** i for i in range(20)],
    xlabel="len(A)",
)

Output:

numpy array concatenate performance comparison

As you can see, the np.concatenate() out-performs the other approaches when the array sizes are small. However, the differences get smaller and smaller as the array size increases.

Conclusion

Today you learned how to concatenate 1D NumPy arrays.

To recap, use the numpy.concatenate() function to join two arrays together, by providing the arrays as a list to the function.

Also, there are 3 alternative approaches:

  • numpy.r_
  • numpy.stack.reshape
  • numpy.hstack

Notice that all these approaches use the numpy.concatenate() behind the scenes.

Thanks for reading.

Happy coding!

Further Reading

NumPy Array Append

Scroll to Top