The Wayback Machine - https://web.archive.org/web/20240114220726/https://www.geeksforgeeks.org/numpy-append-python/
Open In App
Related Articles

numpy.append() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The numpy.append() appends values along the mentioned axis at the end of the array Syntax : 

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

Parameters : 

array   : [array_like]Input array. 
values  : [array_like]values to be added in the arr. Values should be 
     shaped so that arr[...,obj,...] = values. If the axis is defined values can be of any
     shape as it will be flattened before use.
axis    : Axis along which we want to insert the values. By default, array
     is flattened.    

Return : 

An copy of array with values being appended at the end as per the mentioned object
along a given axis. 

Code 1 : Appending arrays 

Python





Output : 

1D arr1 :  [0 1 2 3 4]
Shape :  (5,)

1D arr2 :  [ 8  9 10 11]
Shape :  (4,)

Appended arr3 :  [ 0  1  2  3  4  8  9 10 11]

The time complexity of the numpy.append() function is O(n) where n is the number of elements being appended. This means that the time needed to append elements increases linearly with the number of elements being appended.

The space complexity of the numpy.append() function is also O(n) where n is the number of elements being appended. This means that the amount of space needed to append elements increases linearly with the number of elements being appended.

Code 2 : Playing with axis 

Python




# Python Program illustrating
# numpy.append()
 
import numpy as geek
 
#Working on 1D
arr1 = geek.arange(8).reshape(2, 4)
print("2D arr1 : \n", arr1)
print("Shape : ", arr1.shape)
 
 
arr2 = geek.arange(8, 16).reshape(2, 4)
print("\n2D arr2 : \n", arr2)
print("Shape : ", arr2.shape)
 
 
# appending the arrays
arr3 = geek.append(arr1, arr2)
print("\nAppended arr3 by flattened : ", arr3)
 
# appending the arrays with axis = 0
arr3 = geek.append(arr1, arr2, axis = 0)
print("\nAppended arr3 with axis 0 : \n", arr3)
 
# appending the arrays with axis = 1
arr3 = geek.append(arr1, arr2, axis = 1)
print("\nAppended arr3 with axis 1 : \n", arr3)


Output : 

2D arr1 : 
 [[0 1 2 3]
 [4 5 6 7]]
Shape :  (2, 4)

2D arr2 : 
 [[ 8  9 10 11]
 [12 13 14 15]]
Shape :  (2, 4)

Appended arr3 by flattened :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Appended arr3 with axis 0 : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Appended arr3 with axis 1 : 
 [[ 0  1  2  3  8  9 10 11]
 [ 4  5  6  7 12 13 14 15]]

References : https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.append.html#numpy.append .


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials