How to append numpy arrays
numpy.append() is used to append values to the end of an array. It takes in the following arguments:
-
arr:valuesare attached to a copy of this array. -
values: these values are appended toarr. -
axis: this is an optional parameter that specifies the axis along with which values are appended. If the axis is not specified, thenarrandvaluesare flattened out.
numpy.append()does not alter the original array, instead, it returns a new array.
Take a look at the function signature below:
Code
In the first code snippet, the axis is not specified, so arr and values are flattened out.
import numpy as npprint(np.append([1, 2, 3], [['a', 'b', 'c'], [7, 8, 9]]))
In the following code snippet, values are appended along axis 1.
import numpy as npprint(np.append([[4, 5, 6]], [[7, 8, 9]], axis = 1))# Try setting the axis to 0
For more details, refer to the official documentation.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved