Python List remove() method [With Examples]

In this Python tutorial, we will discuss the Python List extend() method. It is an essential method of the list class that allows us to remove elements from the list.

List remove() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List remove() method
  • Syntax of the remove() method
  • Purpose and use cases of the remove() method

Python List remove() method

The remove() method is a built-in list function in Python that removes the first occurrence of a specified element from the list.

Its syntax is as follows:

list.remove(element)

Here, ‘list’ is the list object from which you want to remove the element, and ‘element’ is the element you want to remove.

Note: If the specified element is not present in the Python list, the remove() method will raise a ValueError.

remove() method in Python List Examples

Example#1

Suppose we have a list of U.S. states and we want to remove a specific state from the Python list. We can use the remove() method to achieve this:

states = ["California", "Texas", "New York", "Florida", "Illinois", "Pennsylvania"]
states.remove("New York")
print(states)

In this example, we have a Python list called states, which contains the names of six U.S. states. We then use the remove() method to remove “New York” from the Python list.

Output:

Python List remove method
Python List remove method

Example#2

We have a Python list of popular U.S. movies, and we want to remove a particular movie from the list:

movies = ["The Godfather", "Pulp Fiction", "Forrest Gump", "Shawshank Redemption", "The Dark Knight"]
movies.remove("Forrest Gump")
print(movies)

In this example, we start with a Python list called movies, which contains the names of five famous films. We then use the remove() method to delete “Forrest Gump” from the Python list.

Output:

List remove method in Python
List remove method in Python

Read: How to delete an element from a Python list

Example#3

We have a Python list of popular sports played in the U.S., and we want to remove a specific sport from the list:

sports = ["Basketball", "Football", "Baseball", "Soccer", "Hockey"]
sports.remove("Baseball")
print(sports)

In this example, we have a Python list called sports containing the names of five different sports. We want to remove “Baseball” from this list, so we use the Python remove() method.

Output:

Python List remove method example
Python List remove method example

Example#4

As mentioned earlier, it only removes the first occurrence of the element. Let’s see an example:

car_brands = ["Ford", "Chevrolet", "Tesla", "Dodge", "Tesla"]
car_brands.remove("Tesla")
print(car_brands)

In this example, we have a Python list of US-based car brands, and we use the remove() method to remove the first occurrence of “Tesla” from the Python list.

Output:

List remove method in Python example
List remove method in Python example

Conclusion

The Python list remove() method is a valuable tool for managing and modifying lists. It enables us to remove the first occurrence of a specified element from a list, while also providing flexibility for handling non-existent elements and removing all instances of a target element.

You may also like to read the following articles: