How to remove the last element from the Python list [4 methods]

In this Python tutorial, we will see how to remove the last element from the Python list in many different ways with demonstrative examples.

Python lists are used to store multiple items in a single variable. Lists are created using square brackets[]. They are mutable, which means we can change their contents – add, modify, or delete elements. For example:

countries = ['America', 'Australia', 'Canada', 'Russia', 'India', 'China']

This is a Python List of countries. Let’s explore different ways of how to remove the last element in a list Python.

Removing the last element from the Python list

There are several different ways to remove the last element from the Python list. They are:

  • pop()
  • List slicing
  • del statement
  • List comprehension

Method-1: Remove the last element from the Python list using the pop() method.

The built-in Python function pop() removes and returns the item at a given index. If no index is provided, it removes the last item in the list.

For example, we try to remove the last film from a Python list of Oscar winners for Best Picture in the last five years using the pop() method.

best_pictures = ["Nomadland", "Parasite", "Green Book", "The Shape of Water", "Moonlight"]

best_pictures.pop()
print(best_pictures)

The output is:

['Nomadland', 'Parasite', 'Green Book', 'The Shape of Water']
remove last element from list python

This way we can use the pop() method to remove the last element of the Python list.

Method 2: Remove the last element method from the Python list using the list-slicing

Python list slicing allows us to create a new list from a subset of an existing Python list. We can create a new list that excludes the last item by omitting the end index.

For example: Let’s check the way to remove the last destination from a Python list of top U.S. travel destinations using list slicing list remove last element Python.

travel_destinations = ["New York City", "Los Angeles", "Grand Canyon", "Las Vegas", "Miami"]

travel_destinations = travel_destinations[:-1]
print(travel_destinations)

The output is: As we are using the same variable name the result after using the list slicing will overwrite the previous saved list. And then, it will print the final Python list with what we need.

['New York City', 'Los Angeles', 'Grand Canyon', 'Las Vegas'] 
python remove last element from list

This way we can use the list slicing to remove the last element of the Python list.

Method 3: Remove the last element using the del statement from the Python list

The del statement in Python removes an item from a list so can be used for a Python list remove last element.

For example: let’s see how to remove the last element from a list of American automotive companies using the del statement

auto_companies = ["General Motors", "Ford", "Tesla", "Rivian", "Lucid Motors"]

del auto_companies[-1]
print(auto_companies)

The output is:

['General Motors', 'Ford', 'Tesla', 'Rivian']
How to remove the last element from the Python list

This way we can use the del statement to remove the last element of the Python list.

Method 4: Remove the last element using the list comprehension from the Python list

List comprehension is a Pythonic way to handle lists. It offers a shorter syntax when we want to create a new list based on the values of an existing Python list using conditional statements.

For example, Let’s try to remove the last element from a list of the biggest tech companies in the U.S. (the “Big Five”) using Python list comprehension.

big_five = ["Apple", "Microsoft", "Amazon", "Google", "Facebook"]

big_five = [company for company in big_five if company != big_five[-1]]
print(big_five)

The output is: As we are using the same variable name the result after using the list comprehension will overwrite the previous saved list. And then, it will print the final Python list with what we need.

['Apple', 'Microsoft', 'Amazon', 'Google']
remove last element from list python

This way we can use list comprehension to remove last element from Python list.

Note: We can use a Positive or Negative Index number to remove the last element of the Python list.

Conclusion:

In conclusion, Python’s list of data structures offers numerous methods to manipulate data according to our needs. The process to Remove the last element from a Python list is a common operation that we can perform using various approaches like the pop() method, list slicing, the del statement, and list comprehension, each with its unique benefits.

You may also like to read: