Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Deleting Tuples in Python: Removing References to Tuples | Mastering Python Tuples
Python Data Structures
course content

Kursinnehåll

Python Data Structures

Python Data Structures

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

book
Deleting Tuples in Python: Removing References to Tuples

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
Uppgift

Swipe to start coding

The animal movies didn't appeal to the audience, except for the animated movie "Finding Nemo".

  • Convert the tuple movie_poster into a list and assign it to the variable temp_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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
toggle bottom row

book
Deleting Tuples in Python: Removing References to Tuples

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
Uppgift

Swipe to start coding

The animal movies didn't appeal to the audience, except for the animated movie "Finding Nemo".

  • Convert the tuple movie_poster into a list and assign it to the variable temp_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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt