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

Course Content

Python Data Structures

Python Data Structures

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

book
Deleting a Tuple

A tuple in Python is immutable, meaning once it's created, you cannot change, add, or remove its elements. However, you can delete the entire tuple using the del statement.

1234567
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Deleting the tuple del movies # Attempting to print the deleted tuple will raise an error print(movies)
copy

Removing Items

Note

Since tuples are immutable, you cannot directly remove items from them. However, you can work around this by converting the tuple into a list, modifying the list, and then converting it back into a tuple.

12345678910111213
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Convert the tuple to a list movies_list = list(movies) # Remove specific items movies_list.remove("Tenet") movies_list.remove("Dunkirk") # Convert the list back to a tuple movies = tuple(movies_list) print(movies)
copy
Task
test

Swipe to show code editor

Suppose you have the following tuple of movie genres:

You want to remove the following genres: "Horror", "Thriller", and "Drama".

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 3. Chapter 4
toggle bottom row

book
Deleting a Tuple

A tuple in Python is immutable, meaning once it's created, you cannot change, add, or remove its elements. However, you can delete the entire tuple using the del statement.

1234567
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Deleting the tuple del movies # Attempting to print the deleted tuple will raise an error print(movies)
copy

Removing Items

Note

Since tuples are immutable, you cannot directly remove items from them. However, you can work around this by converting the tuple into a list, modifying the list, and then converting it back into a tuple.

12345678910111213
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Convert the tuple to a list movies_list = list(movies) # Remove specific items movies_list.remove("Tenet") movies_list.remove("Dunkirk") # Convert the list back to a tuple movies = tuple(movies_list) print(movies)
copy
Task
test

Swipe to show code editor

Suppose you have the following tuple of movie genres:

You want to remove the following genres: "Horror", "Thriller", and "Drama".

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 3. Chapter 4
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