Course Content
Python Data Structures
Python Data Structures
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.
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Deleting the tuple del movies # Attempting to print the deleted tuple will raise an error print(movies)
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.
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)
Swipe to start coding
The animal movies didn't appeal to the audience, except for the animated movie "Finding Nemo"
.
Your goal:
- Convert the tuple
movie_poster
into a list and assign it to the variabletemp_list
. - Remove the elements
"The Lion King"
and"Jurassic Park"
from the list. - Convert the list back into a tuple and assign the value to the variable
movie_poster
. - Delete the list
temp_list
.
Solution
Thanks for your feedback!
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.
movies = ("Inception", "Interstellar", "Tenet", "Dunkirk", "Memento") # Deleting the tuple del movies # Attempting to print the deleted tuple will raise an error print(movies)
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.
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)
Swipe to start coding
The animal movies didn't appeal to the audience, except for the animated movie "Finding Nemo"
.
Your goal:
- Convert the tuple
movie_poster
into a list and assign it to the variabletemp_list
. - Remove the elements
"The Lion King"
and"Jurassic Park"
from the list. - Convert the list back into a tuple and assign the value to the variable
movie_poster
. - Delete the list
temp_list
.
Solution
Thanks for your feedback!