Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.concatenate

Joins a sequence of arrays along an existing axis. Inputs must match in every other dimension; axis=None flattens them before joining.

Since
2.0 (numpy.concat shorthand noted on the page)

How it works

numpy.concatenate combines a sequence of array-like objects along an existing axis. Inputs must match in every dimension except the selected axis; setting axis to None flattens them first, while dtype and casting control the result's type conversion.

Syntax

numpy.concatenate(*arrays, axis=0, out=None, dtype=None, casting='same_kind')

Parameters

NameTypeDescription
arrays (a1, a2, …)sequence of array_likeSequence of array-like objects to join. Shapes must match except along axis.
axisint or None, optionalAxis along which to join the arrays; None flattens inputs first.
outndarray, optionalDestination array for the result; its shape must be correct.
dtypedtype, optionalData type for the result.
casting{'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optionalRules governing data-type casting.

Returns

res: ndarray — the concatenated array.

Key points

  • The function combines multiple array-like inputs into one array along a chosen existing dimension.
  • Without an axis argument, concatenation occurs along dimension 0.
  • An out array can receive the result, but its shape must equal the normally produced result shape.
  • The return value is the resulting concatenated ndarray.
  • The default casting rule is same_kind when dtype conversion is involved.
  • NumPy 2.0 introduced numpy.concat as a shorthand alternative.

Common mistakes

  • Arrays may differ only along the selected concatenation axis; mismatches elsewhere are invalid.
  • axis=None discards the original dimensional layout by flattening each input before joining.
  • When using MaskedArray inputs, the result is masked but their individual masks are not retained; use ma.concatenate when mask preservation is required.

Example: Join arrays along an axis

import numpy as np

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

rows = np.concatenate((a, b), axis=0)
flat = np.concatenate((a, b), axis=None)

print(rows)
print(flat)

Combines compatible arrays by rows, then demonstrates flattening all inputs before concatenation.

When to use it

Append along a batch axis

Combine batches or partitions that share feature dimensions while increasing their sample dimension.

Join columns

Merge compatible matrices horizontally when their row counts match.

Flatten and combine

Turn several compatible inputs into one one-dimensional sequence.

Interview questions

QWhat does numpy.concatenate do, and what shape constraints apply?

It joins a sequence of arrays along an existing axis, using axis=0 by default. Every dimension other than the chosen axis must have matching size.

Follow-up: How does axis=None change the operation? It flattens the arrays before concatenating them, so the result is one-dimensional.

QWhat is the effect of the axis parameter?

axis selects the dimension along which arrays are joined. If it is None, each array is flattened before the operation; otherwise the default is axis 0.

Follow-up: Why might concatenating two 2-D arrays on axis=1 fail? Their sizes must still match on axis 0, because only the selected axis may differ.

QHow do out, dtype, and casting affect the operation?

out supplies the destination ndarray and must have the shape that concatenate would produce without out. dtype requests the result data type, while casting specifies the permitted conversion policy.

Follow-up: What is the default casting policy? It is 'same_kind', which permits conversions within compatible NumPy type kinds rather than arbitrary unsafe conversions.

  • numpy.concat
  • numpy.stack
  • numpy.block
  • numpy.ma.concatenate

Source: numpy.concatenate #. Checked 2026-10-15.