NumPy
Python
arrays
data manipulation
programming

Concatenating two one-dimensional NumPy arrays

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Concatenating two one-dimensional NumPy arrays means building one new array whose values are the first array followed by the second. The operation is simple, but NumPy has several similar-looking functions, and the differences become important once you care about shape, dtype, or performance.

The Normal Answer: np.concatenate

For two 1D arrays, the standard solution is np.concatenate:

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

Output:

text
[1 2 3 4 5 6]

Because the inputs are one-dimensional, NumPy joins them along axis 0, which is the only axis they have.

Equivalent Helpers for the 1D Case

Some other helpers also work for simple 1D input:

python
1import numpy as np
2
3a = np.array([1, 2, 3])
4b = np.array([4, 5, 6])
5
6print(np.hstack((a, b)))
7print(np.r_[a, b])

For one-dimensional arrays, these behave much like np.concatenate. Many developers still prefer np.concatenate because it states the operation explicitly and generalizes more clearly to higher-dimensional arrays.

Concatenation Is Not the Same as Stacking

This is the most common confusion. Concatenation extends an existing axis. Stacking creates a new axis.

python
1import numpy as np
2
3a = np.array([1, 2, 3])
4b = np.array([4, 5, 6])
5
6print(np.concatenate((a, b)).shape)
7print(np.stack((a, b)).shape)

Output:

text
(6,)
(2, 3)

If your downstream code expects a flat vector, np.stack is the wrong tool.

Dtype Rules Still Matter

NumPy may promote the dtype when the inputs differ:

python
1import numpy as np
2
3a = np.array([1, 2, 3], dtype=np.int64)
4b = np.array([4.5, 5.5, 6.5], dtype=np.float64)
5
6result = np.concatenate((a, b))
7print(result.dtype)
8print(result)

This usually gives a floating-point result because NumPy chooses a dtype that can represent both inputs safely.

If type stability matters, make the conversion explicit before concatenation.

Lists and Arrays Can Mix, but Be Explicit

NumPy will often coerce Python lists automatically, but explicit conversion is usually clearer:

python
1import numpy as np
2
3a = np.array([1, 2, 3], dtype=np.int64)
4b = [4, 5, 6]
5
6result = np.concatenate((a, np.array(b, dtype=np.int64)))
7print(result)
8print(result.dtype)

This avoids surprises when list content or inferred dtype changes later.

Empty Arrays Are Fine

Concatenating with an empty array works as long as the dtypes are compatible:

python
1import numpy as np
2
3a = np.array([], dtype=np.int64)
4b = np.array([10, 20, 30], dtype=np.int64)
5
6result = np.concatenate((a, b))
7print(result)

That is useful in conditional workflows, though it is not a reason to keep reallocating arrays repeatedly.

Performance: Do Not Concatenate Inside Every Loop Iteration

This pattern is expensive because each concatenation creates a new array:

python
1import numpy as np
2
3result = np.array([], dtype=np.int64)
4
5for chunk in [np.array([1, 2]), np.array([3, 4]), np.array([5, 6])]:
6    result = np.concatenate((result, chunk))

The better pattern is to collect the chunks and concatenate once:

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

That is faster and easier to reason about.

Common Pitfalls

  • Using np.stack when you actually want one flat 1D array.
  • Concatenating repeatedly inside a loop instead of doing it once at the end.
  • Forgetting that NumPy may upcast the dtype when the inputs differ.
  • Assuming concatenation modifies the original array in place. It returns a new array.
  • Moving from 1D examples to higher-dimensional arrays without thinking about axis compatibility.

Summary

  • Use np.concatenate((a, b)) for the normal case of joining two 1D arrays.
  • 'np.hstack and np.r_ also work for simple one-dimensional input.'
  • Concatenation extends an existing axis, while np.stack creates a new one.
  • Be explicit about dtype if type stability matters.
  • For performance, avoid repeated concatenation inside loops.

Course illustration
Course illustration

All Rights Reserved.