Find the last index of an element in a NumPy array in Python

In this text, we will go through the topic “How to find the last index of an element in a NumPy array in Python.” and understand different ways of fetching the index of the last element of a NumPy array with the help of different examples in this tutorial. There are a few easy and interesting methods for finding an element’s last index in a NumPy array in Python programming. So, read this text to brush up on your coding skills.

np.where():

This is the most basic method of finding the index of the last element of a NumPy array in Python programming. Here you just have to put the element whose index you want to find and then in the output, you get the index of that element.

Program to find the last index:

# import the library
import numpy as np
# create a NumPy array
arr=np.array([1,2,3,4,5,6,7,8,9,10])
# using the np.where() method
# here the last element is 10
print("Index of the last element: ", np.where(arr==10)[0][0])
Output:
Index of the last element:  9

Example 3: last index of an item that occurs more than once in the same array

You can also find the last index of an item that occurs more than once in the same array.

The program is given below:

# import the libraray
import numpy as np
# create a NumPy array
arr=np.array([1,2,3,4,5,6,1,7,8,9,7,10,11,12])
# using the np.where() method
print("Index of the last 7 in the array: ", np.where(arr==7)[0][1])
print("Index of the last 1 in the array: ",np.where(arr==1)[0][1])
Output:
Index of the last 7 in the array:  10
Index of the last 1 in the array:  6

Example 2:

In this example, we will once again use the np.where() method to fetch the index of the last item in a NumPy array in Python. Here we will see how to fetch the index of the last element if that element occurs more than one time in that NumPy array.

Program to fetch the index of the last element:

# import the library
import numpy as np
# create a NumPy array
arr=np.array([1,2,3,4,5,6,7,8,9,10,3])
# using the np.where() method
# here the last element is 3
print("Index of the last element: ", np.where(arr==3)[0][1])
Output:
Index of the last element:  10

In this example, there is 3 at index 2 and there is 3 once again at the last index 10. Had we used the first method to find the index of element 3, we would have got 2 in the output but with the help of example 2, we could easily find the index of the last element using the np.where() method.

Example 2: find the index of non-numeric items

You can also find the index of non-numeric items(strings) in a NumPy array with the help of the np.where() method.

The program is given below:

# import the library
import numpy as np
# create a NumPy array
arr=np.array(['mango','nuts','chocolate','ice-cream','pineapple','biscuits','almonds','cherries','mango'])
# using the np.where() method
# here the last element is mango
print("Index of the last element: ", np.where(arr=='mango')[0][1])
Output:
Index of the last element:  8

Leave a Reply

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