×

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm projects to high-level machine-level algorithms. Many web developers and programmers use Python. It has become the most recommended language nowadays. It is a general-purpose language that uses a wide range of applications.

Python language has many Data types, which are used to store data, whereas a tuple is also one of the data types. Tuples store multiple items in a single variable. Tuples are ordered because they maintain the data insertion order. These are unchangeable. Also, we cannot modify any element in a tuple. And tuples allow duplicate values where you can repeat the values of any component present in the tuple. So as we know, once we add elements to a tuple, they will remain unchanged, but there is a way to change a value of a tuple in Python. This article lets us see how we can change a value in a tuple.

To change a value in a tuple, we cannot change a tuple. Still, we can convert this data type into another data type and complete the conversion so we can convert the tuple into a list then change those values in that list cause lists are mutable or changeable. After changing the values, you can convert the list back to the tuple. To understand this, let us look at an example code

x = ("red", "blue", "green","yellow","black","purple", "orange")
#changing tuple into a list
y = list(x)
#changing the values from the list
y[1] = "brown"
y[4] = "white"
#converting list back to tuple
x = tuple(y)
#printing the changed string
print(x)

Output

How to change a value of a tuple in Python

As you can see in the above code, we first created a tuple with the name x containing colors like red, blue, green, yellow, black, purple, and orange. Then, we assigned a new value, y, to be the list form of x (tuple). And as we usually change values in a list, we changed this also using indexing, and we wanted to change the element in the first index with brown and the element in the fourth index with white. After changing the values, we again convert that list back to a tuple, and finally, we print the modified tuple.

After execution of the code, we can see that the element in the first index that was blue got changed into brown, and the element in the fourth index black got changed into white, and again the changed tuple got printed. So this is how we convert elements in a tuple

Let us look at another example code below:

tuple_new = (5, "hello", 2, 8, 4, 4, "bye", 2)


#changing tuple to list
list = list(tuple_new)


#updating the list
list[2] = "world"


#changing back from a list to a tuple
tuple_new = tuple(list)
#printing changed tuple
print(tuple_new)

Output

How to change a value of a tuple in Python

So in this example, we created a valid tuple where this tuple stored many types of data. There is an integer along with a string. This tuple also consists of a duplicate value in it. So now we converted this tuple of name tuple_new into a list. Then we updated that list by changing the element in the second index (2 into “world”), and then we again converted the list back into a tuple, and then we printed the converted tuple. After the execution in the whole tuple, the element in the second index changed into “world,” as we wanted.

Even in changing the values of the tuple, there is no need to maintain the same data type, like string to string or integer to an integer; we can also convert the string into an integer or an integer into a string. There will be no errors in those conversions.

Conclusion

In this way of converting a data type into another data type, we can change values in an unchangeable data type like a tuple. To change the elements in the tuple, we used a list as our primary data type to convert the elements. After changing the elements, we converted the tuple into a list and then converted the elements back to the tuple format. 


Related Topics

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Event Key in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Virtual Environment

In this tutorial, we will understand Virtual Environment in Python. We will understand the need for a virtual environment and also see how to make use of it in python....

3 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Python String Lowercase

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Python input() function

Python input() function The input() function in Python reads a line from input and converts it to a string (stripping a trailing newline). Syntax input([prompt]) Parameter prompt: This function represents a string, depicting a default...

1 minute read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

How to declare array in python

How to declare array in python? Arrays are a great way of storing our numeric values into a variable in continuous locations. For declaring an array in C language, we used...

4 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.