Course Content
Python Data Structures
Python Data Structures
Updating a Tuple
To modify values within a tuple, you can use a technique similar to the one for deletion. Because a tuple is immutable, you can't directly alter its elements without encountering an error.
movie_ratings = (8.8, 9.0, 7.5, 6.8) # Attempting to modify a tuple directly movie_ratings[1] = 8.9 # Output: Error: 'tuple' object does not support item assignment
However, by converting the tuple to a list, you can easily make the desired changes.
current_movies = ("Inception", "Interstellar", "Tenet", "Dunkirk") # Step 1: Convert the tuple to a list movies_list = list(current_movies) # Step 2: Update the second and third movie titles movies_list[1] = "Memento" movies_list[2] = "The Prestige" # Step 3: Convert the list back to a tuple current_movies = tuple(movies_list) print(current_movies)
Swipe to show code editor
You have the following tuple of movie genres:
Modify it to achieve this result:
Thanks for your feedback!
Updating a Tuple
To modify values within a tuple, you can use a technique similar to the one for deletion. Because a tuple is immutable, you can't directly alter its elements without encountering an error.
movie_ratings = (8.8, 9.0, 7.5, 6.8) # Attempting to modify a tuple directly movie_ratings[1] = 8.9 # Output: Error: 'tuple' object does not support item assignment
However, by converting the tuple to a list, you can easily make the desired changes.
current_movies = ("Inception", "Interstellar", "Tenet", "Dunkirk") # Step 1: Convert the tuple to a list movies_list = list(current_movies) # Step 2: Update the second and third movie titles movies_list[1] = "Memento" movies_list[2] = "The Prestige" # Step 3: Convert the list back to a tuple current_movies = tuple(movies_list) print(current_movies)
Swipe to show code editor
You have the following tuple of movie genres:
Modify it to achieve this result:
Thanks for your feedback!
Updating a Tuple
To modify values within a tuple, you can use a technique similar to the one for deletion. Because a tuple is immutable, you can't directly alter its elements without encountering an error.
movie_ratings = (8.8, 9.0, 7.5, 6.8) # Attempting to modify a tuple directly movie_ratings[1] = 8.9 # Output: Error: 'tuple' object does not support item assignment
However, by converting the tuple to a list, you can easily make the desired changes.
current_movies = ("Inception", "Interstellar", "Tenet", "Dunkirk") # Step 1: Convert the tuple to a list movies_list = list(current_movies) # Step 2: Update the second and third movie titles movies_list[1] = "Memento" movies_list[2] = "The Prestige" # Step 3: Convert the list back to a tuple current_movies = tuple(movies_list) print(current_movies)
Swipe to show code editor
You have the following tuple of movie genres:
Modify it to achieve this result:
Thanks for your feedback!
To modify values within a tuple, you can use a technique similar to the one for deletion. Because a tuple is immutable, you can't directly alter its elements without encountering an error.
movie_ratings = (8.8, 9.0, 7.5, 6.8) # Attempting to modify a tuple directly movie_ratings[1] = 8.9 # Output: Error: 'tuple' object does not support item assignment
However, by converting the tuple to a list, you can easily make the desired changes.
current_movies = ("Inception", "Interstellar", "Tenet", "Dunkirk") # Step 1: Convert the tuple to a list movies_list = list(current_movies) # Step 2: Update the second and third movie titles movies_list[1] = "Memento" movies_list[2] = "The Prestige" # Step 3: Convert the list back to a tuple current_movies = tuple(movies_list) print(current_movies)
Swipe to show code editor
You have the following tuple of movie genres:
Modify it to achieve this result: