remove the last element from a python list

Python – Remove Last Element From List

In this tutorial, we will look at how to remove the last element from a Python list with the help of some examples.

remove the last element from a python 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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top