- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How we can update a Python tuple element value?
Python tuple is an immutable object, which means once tuple is created you cannot change, update, add, or remove its values. If we try to update any of its elements, then it will throw the TypeError.
In this article, we will use three different ways like list conversion, slicing, packing, and unpacking to modify a tuple without raising any errors.
Using list conversion
By converting tuple to a list then converting it back, we can update the values in tuple, but this will create a list copy of tuple in memory.
Example
After converting the tuple to list, we updated the value 100 at index position 2, then converted it back to the tuple. List conversion is best for long tuples.
t = (1, 2, 3, 4, 5) print("Original tuple", t) l = list(t) # change tuple to list l[2] = 100 # update a value t = tuple(l) #converting it back to tuple print("Updated tuple", t) print(type(t))
Output
Original tuple (1, 2, 3, 4, 5) Updated tuple (1, 2, 100, 4, 5) <class 'tuple'>
Using tuple slicing
Tuple slicing is best for short tuples, this allows us to insert multiple elements and we can replace a few consecutive elements also.
Example
By using tuple slicing and concatenation we have successfully updated the value 100 at index position 2. For short tuples slicing and concatenation is the fastest operation compared to the list conversion.
t = (1, 2, 3, 4, 5) print("Original tuple", t) # slicing t = t[:2] + (100,) + t[3:] print("Updated tuple", t)
Output
Original tuple (1, 2, 3, 4, 5) Updated tuple (1, 2, 100, 4, 5)
Packing and unpacking
We can change the any element in a tuple using the “*” operator. This technique is called packing and unpacking.
Example
By using packing, unpacking and tuple slicing we have successfully updated a value in tuple. *t[:i], '300', *t[i+1:] the *t[:i] means tuple unpacked upto the ith position, in the same way *t[i+1:] means tuple is packed to the ith+1 position.
t = (1, 2, 3, 4, 5, 6, 7, 8) print("Original tuple", t) i = 4 #index position t = (*t[:i], '300', *t[i+1:]) # update the value 300 at index position 4 print("Updated tuple", t)
Output
Original tuple (1, 2, 3, 4, 5, 6, 7, 8) Updated tuple (1, 2, 3, 4, '300', 6, 7, 8)
Example
Let’s take another example and insert an element at the middle of the existing tuple.
t = tuple("ABCDEFGHIJKLMN0") print("Original tuple", t) i = (len(t)//2) #index position t = (*t[:i], '1000', *t[i+1:]) # update the value 300 at index position 4 print("Updated tuple", t)
Output
Original tuple ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', '0') Updated tuple ('A', 'B', 'C', 'D', 'E', 'F', 'G', '1000', 'I', 'J', 'K', 'L', 'M', 'N', '0')
By using packing and unpacking technique we have successfully inserted an element at the middle of the original tuple.