Remove the Last Element from List in Python

We will discuss how to remove last element from list in python. We are using list.pop() function, del function, and slice operator to remove last element from list. If the original list is [‘C’, ‘C++’, ‘Java’, ‘Python’, ‘Javascript’]. The list after removing last element [‘C’, ‘C++’, ‘Java’, ‘Python’].

Python Remove the Last Element from List

Using list.pop() function

Here, we will remove the last element using the pop() function. pop() is an inbuilt function in Python that removes the element at the given index from the list. If we don’t specify any index, the pop() function removes and returns the last element from the list.

# Python program to remove last element from list

# take list
mylist = ['C', 'C++', 'Java', 'Python', 'Javascript']

# printing original list
print('The original list:', mylist)

# remove last element using list.pop()
mylist.pop()

# printing list without last element
print('The list after removing last elements:', mylist)

Output:-

The original list: [‘C’, ‘C++’, ‘Java’, ‘Python’, ‘Javascript’]
The list after removing last elements: [‘C’, ‘C++’, ‘Java’, ‘Python’]

Note:- If the list is empty as it tries to pop from an empty list then, the pop() function raises an IndexError.

Using del statement

The del operator removes the item or an element at the specified index location from the list, but the del operator does not return the removed element. The del[ -1] removes the last element from the given list. The negative index -1 specifies the last element of the list.

# Python program to remove last element from list

# take list
mylist = ['C', 'C++', 'Java', 'Python', 'Javascript']

# printing original list
print('The original list:', mylist)

# remove last element using del()
del(mylist[-1])

# printing list without last element
print('The list after removing last elements:', mylist)

Output:-

The original list: [‘C’, ‘C++’, ‘Java’, ‘Python’, ‘Javascript’]
The list after removing last elements: [‘C’, ‘C++’, ‘Java’, ‘Python’]

Remove the Last Element from the list in python using the Slicing

This python program performs the same task but in a different way. In this program, we will remove the last element from the given list using a negative index by slicing. By slicing it from the -1 index, we have removed the last element from the list. The mylist[: -1] specifies all the elements of the list except the last one. The negative index -1 specifies the last element. The [:-1] specifies the character at index 0 and goes up to the index before the last one.

# Python program to remove last element from list

# take list
mylist = ['C', 'C++', 'Java', 'Python', 'Javascript']

# printing original list
print('The original list:', mylist)

# remove last element using slicing
remove_list = mylist[:-1]

# printing list without last element
print('The list after removing last elements:', remove_list)

Output:-

The original list: [‘C’, ‘C++’, ‘Java’, ‘Python’, ‘Javascript’]
The list after removing last elements: [‘C’, ‘C++’, ‘Java’, ‘Python’]

Also See:- Remove Last Character from String in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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