Course Content
Python Data Structures
Python Data Structures
Adding Items to a Tuple
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.
# 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)
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.
# 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)
Swipe to show code editor
You currently have the following tuple of movies:
Your goal is to obtain the following tuple by adding more genres:
Thanks for your feedback!
Adding Items to a Tuple
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.
# 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)
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.
# 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)
Swipe to show code editor
You currently have the following tuple of movies:
Your goal is to obtain the following tuple by adding more genres:
Thanks for your feedback!
Adding Items to a Tuple
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.
# 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)
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.
# 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)
Swipe to show code editor
You currently have the following tuple of movies:
Your goal is to obtain the following tuple by adding more genres:
Thanks for your feedback!
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.
# 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)
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.
# 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)
Swipe to show code editor
You currently have the following tuple of movies:
Your goal is to obtain the following tuple by adding more genres: