Open In App

Remove Last Element from List in Python

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task is to write a Python program to remove the last element present in the list and update the original list in Python.

Example:

Input: [“geeks”, “for”, “geeks”]
Output:[“geeks”, “for”]

Input: [1, 2, 3, 4, 5]
Output:[1, 2, 3, 4]

Explanation: Here simply we have to remove the last element present in the list and print the resultant list.

Remove Last Element from List Using pop() method

First, we create a list li and call the pop() methods which remove the last element from the list.

Python3




li = ["Geeks", "For", "Geeks"]
print("Original list: ", li)
 
# call the pop function
# ele stores the last element
# popped ("Geeks" in this case)
ele = li.pop()
 
# print the updated list
print("New list : ", li)


Output

Original list:  ['Geeks', 'For', 'Geeks']
New list :  ['Geeks', 'For']

Time Complexity: O(1)
Auxiliary Space: O(1)

Remove Last Element from List Using Slicing Technique

The slicing technique can also remove the last element from the list. list[:-1] will remove the last element except for all elements.

Python3




# program to delete the last element from the list
list = ["Geeks", "For", "Geeks"]
print("Original list: ",list)
 
# slicing the list
list= list[:-1]
 
# print the updated list
print("New list: ",list)


Output:

Original list:  ['Geeks', 'For', 'Geeks']
New list: ['Geeks', 'For']

Time Complexity: O(n)
Auxiliary Space: O(1)

Remove Last Element from List Using del operator

del operator can delete the last element from the list along with index.

Python3




# program to delete the last element from the list
list = ["Geeks", "For", "Geeks"]
print("Original list: ",list)
 
# using the del operator
del list[-1]
 
# print the updated list
print("New list: ",list)


Output:

Original list:  ['Geeks', 'For', 'Geeks']
New list: ['Geeks', 'For']

Remove Last Element from List Unpacking Technique

Python3




li = ["Geeks", "For", "Geeks"]
print(*li)
*li, _ = li
print(li)


Output:

Geeks For Geeks
['Geeks', 'For']

Explanation:

Here we have the star(*) operator that unpacks the sequence or iterables into positional arguments. And then underscore(_) ignores the last value and finally assigns it to the list.

Note:

del and pop are similar but the only difference is, in del the removed element is not returned whereas it is done in pop method.

Remove Last Element from islice()

Python3




# Using itertools.islice()
 
from itertools import islice
 
li = ["Geeks", "For", "Geeks"]
print("Original list: ",li)
 
li = list(islice(li, len(li)-1))
 
print("New list: ",li)
#This code is contributed by Edula Vinay Kumar Reddy


Output

Original list:  ['Geeks', 'For', 'Geeks']
New list:  ['Geeks', 'For']

Explanation:

The itertools.islice() function can be used to slice the list and remove the last element. Here, the islice function is used to return an iterator that produces the items from the list starting from the first item to the second to the last item, by specifying the start and stop indices as 0 and len(li)-1 respectively. This new list is then assigned back to the original list li.

Time Complexity: O(n)
Auxiliary Space: O(n)

Remove Last Element using List comprehension.

Removes the last element of the list ‘li’ by creating a new list that includes all the elements of the original list except for the last one. The new list is then assigned to the variable ‘li’.

Python3




li = ["Geeks", "For", "Geeks"]
print("Original list: ",li)
 
li = [x for x in li[:-1]]
print("New list: ",li)
#This code is contributed by tvsk


Output

Original list:  ['Geeks', 'For', 'Geeks']
New list:  ['Geeks', 'For']

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the list.

Conclusion,

Here we have seen some of the possible methods of removing the last element from the list, we can also get some more methods like reversing the array and removing the first character and so on but here also the concept is same.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads