Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the remove() Method: Deleting Specific Elements from Lists | Mastering Python Lists
Python Data Structures
course content

Kursusindhold

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the remove() Method: Deleting Specific Elements from Lists

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:

python

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.

Opgave

Swipe to start coding

You are continuing to work with the travel_wishlist list.

  • Remove the elements "Oslo" and "Sydney" from the list.
  • Use the remove() method to remove these elements.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 9
toggle bottom row

book
Using the remove() Method: Deleting Specific Elements from Lists

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:

python

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.

Opgave

Swipe to start coding

You are continuing to work with the travel_wishlist list.

  • Remove the elements "Oslo" and "Sydney" from the list.
  • Use the remove() method to remove these elements.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 9
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt