How to Remove Last Element From List In Python

Are you looking for how to remove the last element from List in Python? In this article, I will show you different ways you can use to remove the last element from the list and how you can achieve it in the most optimized and faster way.

You can achieve deletion of the last element from the list in three ways. Let me list down all the ways that you can use it right now.

Remove Last Element From List Using Del in Python

The del statement can be used to remove an item from a list based on its index, rather than its value. Unlike the pop() method, which yields a result, this one doesn’t return anything.

In addition to removing slices from a list, the del statement also allows you to erase the list entirely. Let us see in the below example code how you can use the del statement to remove the last element using the index of the list.

#Initializing a List in Python
givenList = [10,11,12,13,14,15]

print("List Before Removing the Last Element")
print(givenList)

#Remove the Last Element from 
#the Given List Using Index
del givenList[-1]

#Printing the Elements in Python Remaining
print("List After Removing the Last Element")
print(givenList)

Output:

List Before Removing the Last Element
[10, 11, 12, 13, 14, 15]
List After Removing the Last Element
[10, 11, 12, 13, 14]

As you can see in the above code, we are using the last element of the list by using the [-1] an index that represents the last indexed element in the list in Python.

Using pop() Method of the List to Remove Last Element

In the Python List class, there is a method called pop() that can be used to remove the last element of the list in Python.

It also Returns the item that was removed from the list at the specified location. It removes the final item in the list and returns nil if no index is provided. Let us see the how pop() method works in the actual Code.

#Initializing a List in Python
givenList = [10,11,12,13,14,15]

print("List Before Removing the Last Element")
print(givenList)

#Remove the Last Element from 
#the Given List Using Pop() Method
returnedVal = givenList.pop()

#Printing the Elements in Python Remaining
print("List After Removing the Last Element")
print(givenList)
print(returnedVal)

Output:

List Before Removing the Last Element
[10, 11, 12, 13, 14, 15]
List After Removing the Last Element
[10, 11, 12, 13, 14]
15
How To Remove A Specific item From An Array
How To Remove A Specific item From An Array

As you can see in the above code, the pop() method removes the last element 15 from the list and it also returns the last element in the variable returnedVal and 15 is stored in that variable.

Using Slicing to Remove the Element from Array or List in Python

Slicing is a method in Python that can be used to slice the list Python. So, suppose you have a list and you want to divide it into two equal copy then you can use the slicing method to create two copies of the list that will have each half of the main list.

Let us see in the below-working code to understand how slicing works and how you can use it to remove the last element from the list in Python.

#Initializing a List in Python
givenList = [10,11,12,13,14,15]

print("List Before Removing the Last Element")
print(givenList)

#Remove the Last Element from 
#the Given List Using Slicing Method
slicedList = givenList[:-1]

#Printing the Elements in Python Remaining
print("List After Removing the Last Element")
print(slicedList)

Output:

In the above code, we are creating a copy of the list given a list by excluding the last element in the new list named slicedList. The method used inside [] brackets with a colon is called slicing in Python.

And here -1 is indicating that copy the givenList excluding the last element in the List. This method creates an extra copy of the list and unless you do not need a copy this method should not be used.

Wrap Up

That is all for today on how to remove the last element from the list in Python. If you have any other solution from the one above mentioned then please let me know in the comment section. And I will be happy to add it here with proper credits.

If you liked our answer then please follow us on FacebookTwitter and Subscribe to our Newsletter to join a great community of developers around the world. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. CentOS 7.7 and Python 3 Installation and Source
  2. Why does pip3 install in ~/.local on Debian? Python
  3. How to Make python3.7 default on Unix/Linux
  4. Stop/Kill a process from the command line after a certain amount of time Python

Leave a Reply

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