Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Deleting Elements in Python Lists: Removing Items Safely | Mastering Python Lists
Python Data Structures
course content

Kurssisisältö

Python Data Structures

Python Data Structures

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

book
Deleting Elements in Python Lists: Removing Items Safely

The del keyword allows you to delete an element at a specific index in the list. This is useful when you know the exact position of the item you want to remove.

The syntax of del keyword is:

python
  • The statement starts with the keyword del;
  • Next, you typically identify the list item with its index, like list[index].

Let's say you have a list of cities in your travel itinerary:

python

You've decided to remove the city "Sydney", which is the last destination on your list. Using del and negative indexing:

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Remove the last city del travel_wishlist[-1] print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Kyoto']
copy

Now, the list is down to three cities:

You can remove any item from the list by using its index number.

Tehtävä

Swipe to start coding

You are working with the travel_wishlist list.

  • Remove the "Paris" and "Kyoto" cities from your travel_wishlist.
  • Use the del keyword two times to do this.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 8
toggle bottom row

book
Deleting Elements in Python Lists: Removing Items Safely

The del keyword allows you to delete an element at a specific index in the list. This is useful when you know the exact position of the item you want to remove.

The syntax of del keyword is:

python
  • The statement starts with the keyword del;
  • Next, you typically identify the list item with its index, like list[index].

Let's say you have a list of cities in your travel itinerary:

python

You've decided to remove the city "Sydney", which is the last destination on your list. Using del and negative indexing:

12345
travel_wishlist = ["Paris", "Oslo", "Kyoto", "Sydney"] # Remove the last city del travel_wishlist[-1] print(travel_wishlist) # Output: ['Paris', 'Oslo', 'Kyoto']
copy

Now, the list is down to three cities:

You can remove any item from the list by using its index number.

Tehtävä

Swipe to start coding

You are working with the travel_wishlist list.

  • Remove the "Paris" and "Kyoto" cities from your travel_wishlist.
  • Use the del keyword two times to do this.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 8
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt