Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The remove() Method | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
The remove() Method

The remove() method deletes the first occurrence of a specific value in the list. This is particularly useful when you know the element's value but not its index.

The syntax of remove() method is:

Now, you decide to remove "Kyoto" from your list because you've already visited it. Here's how you can do it:

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Remove a specific city travel_wishlist.remove("Kyoto") print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Sydney']
copy

If "Kyoto" isn't on the list, this code will raise an error.

12345
travel_wishlist = ["Paris", "Oslo", "Rome", "Sydney"] # Remove a specific city travel_wishlist.remove("Kyoto") print(travel_wishlist) # ValueError: list.remove(x): x not in list
copy

To avoid this, you can check if the city exists before removing it:

123456
travel_wishlist = ["Paris", "Oslo", "Rome", "Sydney"] if "Kyoto" in travel_wishlist: travel_wishlist.remove("Kyoto") print(travel_wishlist)
copy

Note

With the remove() method, you can only take out one item at a time.

Task
test

Swipe to show code editor

You changed your mind about one of the cities. Use the remove() method to delete Oslo from the travel_wishlist.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 9
toggle bottom row

book
The remove() Method

The remove() method deletes the first occurrence of a specific value in the list. This is particularly useful when you know the element's value but not its index.

The syntax of remove() method is:

Now, you decide to remove "Kyoto" from your list because you've already visited it. Here's how you can do it:

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Remove a specific city travel_wishlist.remove("Kyoto") print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Sydney']
copy

If "Kyoto" isn't on the list, this code will raise an error.

12345
travel_wishlist = ["Paris", "Oslo", "Rome", "Sydney"] # Remove a specific city travel_wishlist.remove("Kyoto") print(travel_wishlist) # ValueError: list.remove(x): x not in list
copy

To avoid this, you can check if the city exists before removing it:

123456
travel_wishlist = ["Paris", "Oslo", "Rome", "Sydney"] if "Kyoto" in travel_wishlist: travel_wishlist.remove("Kyoto") print(travel_wishlist)
copy

Note

With the remove() method, you can only take out one item at a time.

Task
test

Swipe to show code editor

You changed your mind about one of the cities. Use the remove() method to delete Oslo from the travel_wishlist.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 9
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt