In this article, we will take a look at 3 ways to remove last list element with example programs and their explanation.

Method 1: Remove last element with pop()

Python list pop() method removes and returns the last element from the list.
pop() accepts an integer and removes the element at that index. When no argument is provided, it removes the last element.
pop() will raise an IndexError if the list is empty or the index is invalid.

Python docs for pop() state,

Remove and return item at index (default last)

Example program follows

numbers=[1,2,3,4]
print ('Original List:', numbers)
deleted=numbers.pop()
print('List with last element removed', numbers)
print('Element removed is:', deleted)

Output is

Original List: [1, 2, 3, 4]
List with last element removed [1, 2, 3]

Element removed is: 4

Note that pop() modifies the original list.

Method 2: Remove last list element with del

Python del statement is used to remove an element from the list by its index. del followed by the element selected with its index will delete it from the list.

Example,

numbers=[1,2,3,4]
print ('Original List:', numbers)
del numbers[-1]
print('List with last element removed', numbers)

Here, numbers[-1] will return the last list element and del before it will delete the element with this value from the list.
Output is

Original List: [1, 2, 3, 4]

List with last element removed [1, 2, 3]

Remember that del will delete only the element at given index. Don’t assume that del numbers[-1] will translate to del 4 and it will delete all the elements with this value.
So, del numbers[1] will delete the element at index 1 from the list.

del does not return the element removed unlike pop().
del modifies the original list, unlike slicing.

Method 3: Remove last list element with slicing

Python list slicing allows you to create a sub-list from a list by selecting elements from the list in a range.
List slicing has below syntax

 

object_name[start:stop:step]

where,

  • object_name: Variable that refers to a list.
  • start: Index where slicing starts. It is optional and defaults to 0(first list element).
    stop: Index where slicing ends. Element at stop index is not included in the result, that is, element till index (stop – 1) is included in the slice.
    It is optional and defaults to the length of list.
  • step: Interval between the index values. It is optional and defaults to 1.

This will return a list with elements between indexes start and (stop – 1) of the original list. Remember that slicing returns a new list and the original list is not modified.

start and stop indexes can be negative, which means that indexing starts from right to left.

In order to remove the last list element, we need to extract a list from first to second last element.
So, with slicing:
start must be 0, the first list element.
stop must be -1, the last list element.

Thus, it will select all the elements from 0 till the second last element. Example,

numbers = [1,2,3,4]
print ('Original List:', numbers)
numbers = numbers[0:-1]
print('List with last element removed', numbers)

Output will be

Original List: [1, 2, 3, 4]

List with last element removed [1, 2, 3]

Since default value of start is 0, we can omit it as below

numbers = numbers[:-1]

Conclusion

In conclusion, we’ve walked through three efficient methods to remove last list element from list in python:
1. using pop() for a straightforward removal while optionally capturing the discarded element,
2. employing the del statement for a quick and memory-efficient method, and
3. leveraging slicing to create an entirely new list minus the last item.
We encourage you to experiment with the methods discussed and explore additional Python list manipulations to elevate your programming capabilities.