Articles

Add elements to the end of numpy Array using insert, append, concatenate

Add elements to the end of numpy Array using insert, append, concatenate


In this numpy tutorial,  we will discuss about:

  • add element to the end of numpy array using insert method,
  • add element to the end of numpy array using append
  • add element to the end of numpy array using concatenate

Before we proceed to learn about add element to the end of numpy array or insert in numpy array, lets create one numpy array.

 

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.


Create Numpy Array

An array is an one dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.


Now lets see few ways to add element to the end of numpy array.

 

Method-1 : Using insert() to add element to the end of numpy array

By using method insert in numpy array, we will add element to the end of numpy array.

Syntax:

numpy.insert(array_data,len(array_data), element)

where,

  1. array_data is the input numpy array
  2. len(array_data) refers to the size of the array
  3. element is the value appended to this array.

Exampleadd element to the end of numpy array

In this add element to numpy example, we are goign to add some elements to the array.

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

#add 34 to the end of the array
array_data = numpy.insert(array_data,len(array_data),34)

print()

#display added array
print(array_data)

print()

#add 78 to the end of the array
array_data = numpy.insert(array_data,len(array_data),78)

#display added array
print(array_data)

Outputadd element to numpy result

From the above example, we have added the values 34 and 78 separately at the end of the numpy array.

[34 56 43 22 45  6 54  2]

[34 56 43 22 45  6 54  2 34]

[34 56 43 22 45  6 54  2 34 78]

Now lets see another method in python add element to end of numpy array.


Method- 2 : Using append() to add element to the end of numpy array

By using append in numpy array, we will add the elements to the numpy array at the end.

Syntax:

numpy.append(array_data , element)

where,

  1. array_data is the input numpy array
  2. element is the value appended to this array.

Exampleinsert element to numpy array

In this insert element to numpy array example, we are going to add two elements to the array.

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

#add 34 to the end of the array
array_data = numpy.append(array_data,34)

print()

#display added array
print(array_data)

print()

#add 78 to the end of the array
array_data = numpy.append(array_data,78)

#display added array
print(array_data)

Outputadd element to numpy using append in numpy array

From the above insert element to numpy array example, we have added the values 34 and 78 separately at the end of the numpy array.

[34 56 43 22 45  6 54  2]

[34 56 43 22 45  6 54  2 34]

[34 56 43 22 45  6 54  2 34 78]

Now lets see another way to insert element to numpy array.


Method -  3 : Using concatenate() insert element to numpy array

By using concatenate in numpy, we will add single/multiple elements to the numpy array at the end at a time.

Syntax:

numpy.concatenate(array_data , [element/s])

where,

  1. array_data is the input numpy array
  2. element is the value appended to this array from a list.

Exampleconcatenate in numpy example to add element to numpy

In this python add element to end of numpy array example, we are going to multiple  elements to the array.

#importing the numpy module
import numpy 

#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

#add 1,2,3,4,5,6,7,8,9 values to the end of the array
array_data=numpy.concatenate((array_data,[1,2,3,4,5,6,7,8,9]))

#display the added array
print(array_data)

Output: python add element to end of numpy array using concatenate in numpy result

From the above example, we have added the values 1 2 3 4 5 6 7 8 9  at a time at the end of the numpy array.

[34 56 43 22 45  6 54  2]
[34 56 43 22 45  6 54  2  1  2  3  4  5  6  7  8  9]

This wraps up our session on add element to the end of numpy array using insert in numpy array, append in numpy array, concatenate in numpy.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1000+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 20,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!