🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.concatenate()

AI & DATA SCIENCE // np-concatenate

np.concatenate() joins a sequence of arrays together along an existing axis, without adding any new dimension.

Syntax

np.concatenate((arr1, arr2, ...), axis=0)

Deep Dive Course

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a, b)))
localhost:3000

2Practical Example

Here is a real-world application of np.concatenate() showing how it is used in production NumPy code.

editor.html
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))
localhost:3000

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.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a, b)))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

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

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))

Best Practices

  • 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
  • Check that all input arrays already share the same number of dimensions and matching sizes along every non-joined axis before calling concatenate()
  • 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

Interview Question

What's the fundamental difference between np.concatenate() and np.stack(), given both combine multiple arrays into one?

Hint: Think about whether the result gains an extra dimension compared to the inputs.

concatenate() joins arrays along an axis that already exists in each of them, so the resulting array has the same number of dimensions as the original inputs, just larger along the joined axis. stack() instead creates a brand-new axis that didn't exist in the original arrays, and lines the inputs up along it, so the result always has one more dimension than each individual input. Concatenating a list of 1D arrays gives a longer 1D array, while stacking the same list gives a 2D array.

Exercises

MediumPractice using np.concatenate() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.concatenate((a, b)))

Frequently Asked Questions

What's the fundamental difference between np.concatenate() and np.stack(), given both combine multiple arrays into one?

concatenate() joins arrays along an axis that already exists in each of them, so the resulting array has the same number of dimensions as the original inputs, just larger along the joined axis. stack() instead creates a brand-new axis that didn't exist in the original arrays, and lines the inputs up along it, so the result always has one more dimension than each individual input. Concatenating a list of 1D arrays gives a longer 1D array, while stacking the same list gives a 2D array.

Related Functions

np-stacknp-vstacknp-hstack