In this tutorial, we will look at how to remove the last element from a Python list with the help of some examples.
How to remove the last element of a list?
You can use the python list pop()
function or apply a slice operation to remove the last element from a Python list. The following is the syntax for both the methods-
# remove last element using list pop() ls.pop(-1) # remove last element using list slice ls = ls[:-1]
Let’s now look at both these methods with the help of some examples.
Using list pop()
to remove the last element
The list pop()
function is used to remove an element from a list. Pass the index of the element you want to remove as the argument. It modifies the list in place and returns the element removed (or “popped”).
To remove the last element from a list, pass the index of the last element in the list (you can also use a negative index, -1
) as an argument to the list pop()
function. Here’s an example –
# create a list ls = [1, 2, 4, 7, 3] # remove the last element ls.pop(-1) # display the list print(ls)
Output:
[1, 2, 4, 7]
The list now has the last element from the original list removed.
Note that if the list is empty, the pop()
function will return an error.
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.
# create a list ls = [] # remove the last element ls.pop(-1) # display the list print(ls)
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_3740/1758444282.py in <module> 2 ls = [] 3 # remove the last element ----> 4 ls.pop(-1) 5 # display the list 6 print(ls) IndexError: pop from empty list
We get an IndexError
error. This is because we’re trying to pop an element from an empty list.
Using list slice to remove the last element
Slice the list from the start to the last element (but not including the last element) in the list and assign the resulting list to the original list. Using a negative index can be helpful (for example, -1 to indicate the index of the last element). Let’s look at an example.
# create a list ls = [1, 2, 4, 7, 3] # remove the last element ls = ls[:-1] # display the list print(ls)
Output:
[1, 2, 4, 7]
We get the same result as above. Note that unlike the list pop()
function, which modifies the list in-place, here, we are creating a new list using a slice operation and then assigning it to our original list variable.
Also, note, you don’t get an error if you perform this slice operation on an empty list.
# create a list ls = [] # remove the last element ls = ls[:-1] # display the list print(ls)
Output:
[]
We don’t get any errors. The resulting list is also an empty list.
In this tutorial, we looked at the list pop()
function and the list slice operation to remove the last element from the list. You can use either of the two methods depending on your use case. Also, keep in mind the following points about these methods.
- The list
pop()
function modifies the list in place. It gives an error if applied on an empty list. - The list slice method returns a copy of the list with the element removed. It does not give an error when applied on an empty list.
You might also be interested in –
- Python – Remove First Element From List
- Python List Remove, Pop and Clear
- Python Remove Duplicates from a List
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.