concatenate() requires every input array to already have the same number of dimensions, and to match in size along every axis except the one being joined — joining along axis=0, the default, stacks arrays end-to-end along their first dimension, like appending more rows to a 2D array, while axis=1 joins them side by side along the second dimension instead. Unlike np.stack(), which creates a brand-new dimension, concatenate() only extends an existing one, so the result has the same number of dimensions as the inputs.
1Understanding np.concatenate()
concatenate() requires every input array to already have the same number of dimensions, and to match in size along every axis except the one being joined — joining along axis=0, the default, stacks arrays end-to-end along their first dimension, like appending more rows to a 2D array, while axis=1 joins them side by side along the second dimension instead. Unlike np.stack(), which creates a brand-new dimension, concatenate() only extends an existing one, so the result has the same number of dimensions as the inputs.
If you get a ValueError about mismatched dimensions from concatenate(), check that every input array actually has the same shape along every axis except the one you're joining on — even a single mismatched dimension elsewhere will fail.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a, b)))2Practical Example
Here is a real-world application of np.concatenate() showing how it is used in production NumPy code.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))3Best Practices
Follow these guidelines when working with np.concatenate():
1. Use concatenate() when joining arrays should extend an existing axis, not create a new one — reach for np.stack() instead if you need a genuinely new dimension
2. Check that all input arrays already share the same number of dimensions and matching sizes along every non-joined axis before calling concatenate()
3. Prefer np.vstack()/np.hstack() for the common, specific cases of stacking rows or columns, since their names communicate intent more directly than a generic axis argument
Tip: If you get a ValueError about mismatched dimensions from concatenate(), check that every input array actually has the same shape along every axis except the one you're joining on — even a single mismatched dimension elsewhere will fail.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a, b)))