Table of Contents
Python Update Tuples
Python is versatile and is known as a powerful programming language. It will also provide rich data structures for monitoring several types of information. Check out this article to learn more about the Python Update Tuples.
Whereas, tuples are ordered collections with unique characteristics. Tuples are immutable and once the meaning is made, it can’t be altered.
What is Tuple in Python?
In Python, A tuple is referred to as an ordered, immutable sequence of elements. It also has a data structure that will allow you to store the multiple values of different types together.
Furthermore, tuples will work similarly to the list in Python. In the syntax of tuples, it will consist of elements that are surrounded by the parenthesis() and are separated with commas.
How do you change the Tuple Values in Python?
Check out the example to learn how to convert the tuple into the list.
# Original tuple
x = ("apple", "banana", "cherry")
# Convert tuple to list
y = list(x)
# Replace the second element of the list with 'mango'
y[1] = 'mango'
# Display the converted list
print("Original Tuple:")
print(x)
print("\nConverted List:")
print(y)
Output
Original Tuple:
('apple', 'banana', 'cherry')
Converted List:
['apple', 'mango', 'cherry']
What are the Several ways to add the item to the Python Tuple?
In Python, tuples are mostly immutable and do not have a built-in append() method. Some of the ways to add the items to the tuple are provided below:
a. Convert into the list:
Tuples can be changed into the list by adding the item and then, turning it back to the tuple.
# Original tuple
x = ("apple", "banana", "cherry")
# Convert tuple to list
y = list(x)
# Add "grapes" to the list
y.append("grapes")
# Convert the modified list back to tuple
x_modified = tuple(y)
# Display the modified tuple
print("Original Tuple:")
print(x)
print("\nModified Tuple with 'grapes' added:")
print(x_modified)
Output
Original Tuple:
('apple', 'banana', 'cherry')
Modified Tuple with 'grapes' added:
('apple', 'banana', 'cherry', 'grapes')
b. Add tuple to the tuple
In Python, it is possible to add tuples to tuples. Such as adding an item, creating a new tuple with the item, and then adding it to the existing tuple as shown in the example below.
Example
# Original tuple
x = ("apple", "banana", "cherry")
# Create a new tuple with the value "kiwi"
new_tuple = ("kiwi",)
# Concatenate the original tuple and the new tuple
combined_tuple = x + new_tuple
# Display the combined tuple
print("Combined Tuple:")
print(combined_tuple)
Output
Combined Tuple:
('apple', 'banana', 'cherry', 'kiwi')
What are the examples for converting the list back into the Python Tuple?
It is important to remember that removing items is not possible in Tuples of Python. Let us look into the example below:
# Original tuple
x = ("apple", "banana", "cherry")
# Convert tuple to list
y = list(x)
# Remove "banana" from the list
if "banana" in y:
y.remove("banana")
# Convert the modified list back to tuple
x_modified = tuple(y)
# Display the modified tuple
print("Original Tuple:")
print(x)
print("\nModified Tuple with 'banana' removed:")
print(x_modified)
Output
Original Tuple:
('apple', 'banana', 'cherry')
Modified Tuple with 'banana' removed:
('apple', 'cherry')
Another way to delete the items in the tuple is to delete it completely with the help of the del keyword:
thistuple = ("apple", "banana", "mango")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
Output
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(thistuple) # this will raise an error because the tuple no longer exists
NameError: name 'thistuple' is not defined
Conclusion
In Python, tuples are like lists but with one crucial difference: they are immutable, meaning once created, their elements cannot be changed. However, there are still ways to update tuples indirectly. Beginners can improve their skills and knowledge on changing or adding the items of tuples in Python. Several examples are illustrated in this article for their reference.
Python – Update Tuples -FAQs
Q1.Can we update the tuple in Python?
Ans. In Python, the value cannot be changed after the tuple is formed.
Q2.Can you mutate a tuple in Python?
Ans.No, it doesn’t have any private attribute to mutate the tuple in Python.
Q3.How do you append to a tuple?
Ans. A tuple can be appended with the help of the += operator.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together