Concatenating two one-dimensional NumPy arrays
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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:
Output:
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:
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.
Output:
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:
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:
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:
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:
The better pattern is to collect the chunks and concatenate once:
That is faster and easier to reason about.
Common Pitfalls
- Using
np.stackwhen 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.hstackandnp.r_also work for simple one-dimensional input.' - Concatenation extends an existing axis, while
np.stackcreates a new one. - Be explicit about dtype if type stability matters.
- For performance, avoid repeated concatenation inside loops.
Related reading
- Condensed matrix function to find pairs
- Conditional Replace Pandas
- Conditional sampling of binary vectors ?
- Confusion between numpy, scipy, matplotlib and pylab
- Concatenating/Merging/Joining two AVL trees
- Concurrent Dictionary Correct Usage
- Concept of getter in TensorFlow
- Concurrent.futures vs Multiprocessing in Python 3

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.