Remove Last element from a NumPy Array in Python

In this article we will learn how to remove last element from a NumPy Array.

Given a NumPy array, we need to remove last element from a NumPy Array i.e. delete the element at the last index of
the array.

Example:             

    Given array = [ 1, 3, 5, 8, 9 ]
    After removing last element = [ 1, 3, 5, 8 ]
    

There are multiple ways to remove last element from a NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example

1.) Using Slicing

Slicing in python can be defined as taking elements from one given index to another given index.

Example:             
        arr = [ 1, 3, 5, 8, 9 ]
        arr[0 : 2]  ===> this will give [ 1, 3, 5 ]
    

We can get index of last element by decrementing the length of array by one. In the above example the length of array is 5, so the index of the last element is 4 i.e (5-1). Now using Slicing we will remove the last element.

The len() method is used to get the length of array, The all() method takes array as input parameter and returns a integer value.

Syntax of len()

len(array)

Parameters:

array          = The array to be passed to the function.

Returns:

Returns an integer value.   

Approach

1. Import numpy library and create numpy array
2. Using the len() method to get the length of the given array
3. Now use slicing to remove the last element by setting the start of slicing=0 and end = lastIndex
4. lastIndex is calculated by decrementing the length of array by one.

Source code

import numpy as np

# Creating numpy array
arr = np.array([1, 2, 3, 4, 5, 6, 7])

lastElementIndex = len(arr)-1

# Removing the last element using slicing 
arr = arr[:lastElementIndex]

print(arr)

OUTPUT:

[1 2 3 4 5 6]    

2.) Using delete() method

The delete() function is a built-in method in numpy library. The delete() method is used to remove the elements from the given array, the delete() method takes array and a index or array of indices as parameters and returns a array by deleting the elements at given indexes.
Now to remove the last element in the array we need to pass the given array and the index of the last element to the delete method.
The index of the last element is -1, this called negative indexing.

Syntax of delete()

numpy.delete(arr, obj)

Parameters:

arr          = The array to be passed to the function.
obj          = index (or array of index)  of the elements to be deleted.

Returns:

Returns array with the elements removed.    

Approach

1. import numpy library and create numpy array
2. Using the len() method to get the length of the given array
3. Index of the last element is given as -1
4. Now use pass the given array and the index of last element to the delete() method.
5. Print the array.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

# Removing the last element using delete method
arr = np.delete(arr,-1)

print(arr)
    

OUTPUT:

[1 2 3 4 5 6]    

3.) Using Boolean array

The elements in a numpy array can be accessed by passing a Boolean array as index to the array

Example:             

        arr = [ 1, 3, 5, 8, 9 ]
        boolArray = [True, True, False, False, False]
        arr[boolArray]  ===> this will give [ 1, 3 ]
    

Now to remove the last element from the array create a Boolean array with length same as the array. All the elements in this Boolean array are True except for the last element. Then pass this Boolean array as index to the original NumPy Array. This will give an array with last element removed.

Approach

1. import numpy library and create numpy array
2. Create a boolean array with length same as the array and All the elements as True except for the last element
3. Now Pass this boolean array as index to the Given array
4. This will give an array with last element removed.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

booleanIndex = [True, True, True, True, True, True, False]

# Removing the last element using boolean index
arr = arr[booleanIndex]

print(arr)

OUTPUT:

[1 2 3 4 5 6]    

4.) Using index array

The elements in a numpy array can be accessed by passing a index array as index to the array

Example:             

        arr = [ 1, 3, 5, 8, 9 ]
        indexArray = [1,3]
        arr[indexArray]  ===> this will give [ 3, 8 ]
    

Now to remove the last element from the array, create a index array containing indexes of all the elements except for the last element. Then pass this index array as index to the original NumPy Array, This will give an array with last element removed.

Approach

1. Import numpy library and create numpy array
2. Create a index array with elements as indexes of all the elements except for the last element
3. Now Pass this index array as index to the Given array
4. This will give an array with last element removed.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

# Removing the last element using index array
indexArray = [i for i in range(len(arr)-1)]
arr = arr[indexArray]

print(arr)

OUTPUT:

[1 2 3 4 5 6]    

5.) Using resize() method

The resize() method is a built-in method in numpy library. The resize() method is used to resize the given array into specified shape. The resize() method takes array and a shape as parameters and returns a array by resizing the array into specified shape.

Now to delete the last element in the array we need to pass the given array and set the shape as length of array decrement by one.

Syntax of resize()

numpy.resize(arr, new_shape)

Parameters:

        arr                = The array to be passed to the function.
        new_shape          = Shape of resized array.

Returns:

        Returns a array by resizing the array into specified shape.

Approach

1. Import numpy library and create numpy array
2. Using the len() method to get the length of the given array
3. Set shape to length of array decremented by one.
4. Now pass the given array and shape to the resize() method.
5. Print the array.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

# Removing the last element using resize()
arr.resize((len(arr) -1))

print(arr)

OUTPUT:

[1 2 3 4 5 6]    

Summary

We learned about different ways to remove last element from a NumPy Array in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top