remove last element from array numpy
In this numpy tutorial, we will discuss about:
-
remove last element from array numpy using slicing,
-
remove last element from array numpy using delete,
-
remove last element from array numpy using boolean array
Before we go ahead with learning remove last element from array numpy, lets know a bit about numpy.
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module 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.
Array
An array is an one dimensional data structure used to store single data type data. It will only store all integer data or all string type data.or all float type data.
We can create a numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
Problem Scenario:
Input- [12,34,32,56,78,90,65]
Output- [12,34,32,56,78,90]
Lets see few methods to remove last element from array numpy.
Method 1 : Slicing
Here we will use slicing, to remove the last element in an array. Last element position is found by using length of the array -1. So by slicing from 0 th index, we can ignore last element.
Syntax:
array_input[:len(array_input)-1]
where, array_input is the input numpy array.
Example: slice remove last element from numpy array
In this remove last element from array numpy example, we will remove last element through slicing.
#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])
#display the actual numpy array
print(array_data)
#remove last element using slicing
print(array_data[:len(array_data)-1])
Output: From the above code, we created numpy array with 8 elements. last element is 2, so we will exclude this element and display the actual and modified numpy array.
[34 56 43 22 45 6 54 2]
[34 56 43 22 45 6 54]
Lets see another method to remove last element from array numpy.
Method 2 : delete()
Here we will use delete() method available in numpy module to remove the last element in an array.
last element position is found by using length of the array -1. We can delete the last element by specifying this index in the second parameter of the delete() method.
Syntax:
numpy.delete(array_input, len(array_input)-1)
where, array_input is the input numpy array.
Example: remove last element from array numpy using delete()
In this remove last element from array numpy example, we will remove last element using delete() method.
#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])
#display the actual numpy array
print(array_data)
#remove last element using delete()
print(numpy.delete(array_data, len(array_data)-1))
Output: From the above code, we created numpy array with 8 elements. last element is 2, so we will exclude this element and display the actual and modified numpy array.
[34 56 43 22 45 6 54 2]
[34 56 43 22 45 6 54]
Lets see one more method to remove last element from array numpy.
Method 3 - Using Boolean Array
Here we are using Boolean Array that will take two boolean values - True and False - So we can use these values which act as index for the actual numpy array.
False- will take no value
True- will take value
So By specifying the False value in the last position of the boolean array, we can exclude the last value from the numpy array.
Through this boolen array, we can index through actual array.
Syntax:
array_input[boolean_array]
Example: remove last element from array numpy using boolean array
In this remove last element from array numpy example, we will remove last element using boolen array indexing.
#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])
#create the boolean array
b_array=[True, True, True, True, True, True,True,False]
#display the actual numpy array
print(array_data)
#remove last element through indexing boolean array
print(array_data[b_array])
Output: From the above code, we created numpy array with 8 elements. last element is 2, so we will exclude this element and display the actual and modified numpy array.
[34 56 43 22 45 6 54 2]
[34 56 43 22 45 6 54]
This wraps up our session on remove last element from array 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 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Apr 16,2022