In this tutorial, we will look at how to remove the last element from a Numpy array with the help of some examples.
How do I remove the last element of a Numpy Array?
You can use the numpy delete()
function to remove the last element of a numpy array. The following is the syntax –
# remove last element from numpy array ar, assuming numpy imported as np np.delete(ar, len(ar)-1)
The np.delete()
function is used to remove an element using its index. Since we want to remove the last element, we pass its index which is one less than the length of the array. It returns a copy of the original array with the specific element deleted.
Alternatively, you can also slice the original array from its start to the second last element to get a new numpy array with the last element removed from the original array.
Note that numpy arrays are immutable. That is, they cannot be modified after creation.
Steps to remove the last element in Numpy
Let’s now look at a step-by-step example of using the methods mentioned above –
Step 1 – Create the numpy array
First, we will create a numpy array that we will be using throughout this tutorial.
import numpy as np # create numpy array ar = np.array([10, 20, 30, 40, 50]) # print the array print(ar)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[10 20 30 40 50]
Here we use the np.array()
function to create a numpy array from a list of numbers. We now have a numpy array containing the numbers 10, 20, 30, 40, and 50.
Step 2 – Remove the last element from the array
First, let’s use the np.delete()
function to remove the last element of the numpy array ar
created above.
# remove last element ar_new = np.delete(ar, len(ar)-1) # print the returned array print(ar_new) # print the original array print(ar)
Output:
[10 20 30 40] [10 20 30 40 50]
The resulting array does not have the last element from the original array. You can also see that the original array is not modified.
Let’s now use the slicing method to remove the last element.
# remove last element ar_new = ar[:-1] # print the returned array print(ar_new) # print the original array print(ar)
Output:
[10 20 30 40] [10 20 30 40 50]
Here, we slice the original array from its start to the last element (but not including the last element). We use a negative index here for simplicity. We get the same result as above. A copy of the original array with the last element removed.
For more on slicing numpy arrays, refer to numpy’s guide on indexing and slicing.
You might also be interested in –
- How to remove elements from a numpy array?
- Remove First Element From Numpy Array
- Trim zeros from a numpy array in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.