×

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

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

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

4 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

OS Module in Python

What is a module? The Modules are the kind of files that contains python statements and definitions. The module is known by the name of the file followed by the suffix...

8 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python Win32 Process

The Win32 process is essentially a Python procedure. This program provides access to advanced Win32 process creation and management features. Process objects are created through the Create method (the constructor)....

6 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.