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

NumPy · Function

np.append

Appends values to an array and returns a new ndarray. Without axis, both inputs are flattened; with axis, values must have compatible dimensions.

How it works

numpy.append creates and returns a new array containing values after arr. With axis omitted or set to None, both inputs are flattened first; with an axis supplied, values must match arr in every other dimension.

Syntax

numpy.append(arr, values, axis=None)

Parameters

NameTypeDescription
arrarray_likeArray-like object whose contents receive the appended values.
valuesarray_likeArray-like data to append; its shape must be compatible when axis is specified.
axisint, optionalOptional dimension along which to append. If omitted, both inputs are flattened.

Returns

ndarray: a copy of arr with values appended along axis; when axis is None, the result is flattened.

Key points

  • Use numpy.append to place additional values after the existing contents of an array.
  • The function works from a copy of arr rather than extending the existing allocation.
  • The optional axis argument determines which dimension receives the appended data.
  • A missing axis produces a one-dimensional output.

Common mistakes

  • The operation does not modify arr; assigning the returned array is necessary if the extended result should replace the original.
  • Omitting axis can silently discard the original dimensional structure by flattening the inputs.
  • When axis is provided, values must have matching dimensions except along the append axis.

Example: Append a row

import numpy as np

rows = np.array([[1, 2], [3, 4]])
result = np.append(rows, [[5, 6]], axis=0)
print(result)

Adds one row along axis 0 and leaves the input array unchanged.

When to use it

Append along a selected axis

Add records to a two-dimensional dataset while preserving rows.

Extend a vector

Combine one-dimensional data with additional elements.

Flatten and append

Produce a flat sequence from inputs with differing shapes.

Interview questions

QDoes numpy.append modify the input array?

It returns a new ndarray with values placed after arr. The original array is not changed because a new array is allocated.

Follow-up: What happens when axis is None? Both arr and values are flattened, and the result is one-dimensional.

QWhat is the effect of leaving axis unspecified?

With axis omitted, both inputs are flattened before appending. The result is therefore a flattened array.

Follow-up: How do you preserve rows in a two-dimensional array? Supply the axis, such as axis=0, and provide values with a compatible shape.

QWhat shape requirements apply when axis is specified?

axis selects the dimension along which values are appended. Values must match arr in shape for all dimensions other than that axis.

Follow-up: Why might an append fail with axis=0? A non-axis dimension differs, so the values do not have the required compatible shape.

  • numpy.concatenate
  • numpy.stack
  • numpy.ma.append

Source: numpy.append. Checked 2026-10-15.