Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Deleting Elements | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

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

bookDeleting Elements

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:

  • 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:

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.

Task
test

Swipe to show code editor

Here's your list:

groups = ["Paris", "Oslo", "Kyoto", "Sydney", "Rome"]

Remove the first and third cities from your travel_wishlist using the del keyword two times.

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 8
toggle bottom row

bookDeleting Elements

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:

  • 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:

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.

Task
test

Swipe to show code editor

Here's your list:

groups = ["Paris", "Oslo", "Kyoto", "Sydney", "Rome"]

Remove the first and third cities from your travel_wishlist using the del keyword two times.

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 8
toggle bottom row

bookDeleting Elements

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:

  • 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:

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.

Task
test

Swipe to show code editor

Here's your list:

groups = ["Paris", "Oslo", "Kyoto", "Sydney", "Rome"]

Remove the first and third cities from your travel_wishlist using the del keyword two times.

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!

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:

  • 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:

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.

Task
test

Swipe to show code editor

Here's your list:

groups = ["Paris", "Oslo", "Kyoto", "Sydney", "Rome"]

Remove the first and third cities from your travel_wishlist using the del keyword two times.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 8
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