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

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
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.

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
Task
test

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:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6
toggle bottom row

book
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.

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
Task
test

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:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt