Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Adding Items to a Tuple: Alternative Approaches Using Lists | Mastering Python Tuples
Python Data Structures
course content

Contenuti del Corso

Python Data Structures

Python Data Structures

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

book
Adding Items to a Tuple: Alternative Approaches Using Lists

To add elements to a tuple, you can use the same approach that's effective for deletion. Since tuples are immutable, we can't directly add an element to them without encountering an error. However, there are workarounds to add new elements to a tuple. Let's explore two common approaches.

12345678910111213
# Original tuple of movies movies = ("Inception", "Interstellar", "Tenet") # Convert the tuple to a list movies_list = list(movies) # Add a new movie to the list movies_list.append("Dunkirk") # Convert the list back to a tuple movies = tuple(movies_list) print("After:", movies)
copy

Another way to add an element to a tuple is by concatenating it with another tuple. This is something we explored a few chapters back. If you want to add one or more elements, simply create a new tuple with these elements and combine it with the original tuple.

12345678910
# Original tuple of movies movies = ("Inception", "Interstellar", "Tenet") # Create a new tuple with the movie to add new_movies = ("Dunkirk",) # Concatenate the tuples movies += new_movies print(movies)
copy
Compito

Swipe to start coding

You are given the tuple animal_movies.

  • Add two new movies to this tuple: "Dumbo" and "Zootopia".
  • You can use any method to add them, either by converting to a list or by concatenating tuples.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 6
toggle bottom row

book
Adding Items to a Tuple: Alternative Approaches Using Lists

To add elements to a tuple, you can use the same approach that's effective for deletion. Since tuples are immutable, we can't directly add an element to them without encountering an error. However, there are workarounds to add new elements to a tuple. Let's explore two common approaches.

12345678910111213
# Original tuple of movies movies = ("Inception", "Interstellar", "Tenet") # Convert the tuple to a list movies_list = list(movies) # Add a new movie to the list movies_list.append("Dunkirk") # Convert the list back to a tuple movies = tuple(movies_list) print("After:", movies)
copy

Another way to add an element to a tuple is by concatenating it with another tuple. This is something we explored a few chapters back. If you want to add one or more elements, simply create a new tuple with these elements and combine it with the original tuple.

12345678910
# Original tuple of movies movies = ("Inception", "Interstellar", "Tenet") # Create a new tuple with the movie to add new_movies = ("Dunkirk",) # Concatenate the tuples movies += new_movies print(movies)
copy
Compito

Swipe to start coding

You are given the tuple animal_movies.

  • Add two new movies to this tuple: "Dumbo" and "Zootopia".
  • You can use any method to add them, either by converting to a list or by concatenating tuples.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 6
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt