Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Updating Tuples in Python | Mastering Python Tuples
Python Data Structures
course content

Kursinnhold

Python Data Structures

Python Data Structures

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

book
Updating Tuples in Python

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.

123
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
copy

However, by converting the tuple to a list, you can easily make the desired changes.

12345678910111213
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)
copy
Oppgave

Swipe to start coding

You are given a new tuple containing genres, called movie_genres.

  • Convert the tuple into a list and assign it to the variable temp_list.
  • Replace the element "Drama" with "Thriller".
  • Replace the element "Horror" with "Adventure".
  • Convert the list back into a tuple and assign the value to the variable movie_genres.
  • Delete the temporary list.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 5
toggle bottom row

book
Updating Tuples in Python

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.

123
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
copy

However, by converting the tuple to a list, you can easily make the desired changes.

12345678910111213
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)
copy
Oppgave

Swipe to start coding

You are given a new tuple containing genres, called movie_genres.

  • Convert the tuple into a list and assign it to the variable temp_list.
  • Replace the element "Drama" with "Thriller".
  • Replace the element "Horror" with "Adventure".
  • Convert the list back into a tuple and assign the value to the variable movie_genres.
  • Delete the temporary list.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 5
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt