B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Update Python Tuples with Examples | LiveCodeProgramming
LiveCodeProgramming

Update Tuple Items in Python

Tuples are immutable, which means we cannot change their values directly. But we can convert a tuple into a list, update it, and then convert it back to a tuple.

Tuple immutable होता है, यानी आप सीधे इसका data change नहीं कर सकते। लेकिन आप tuple को पहले list में बदल सकते हैं, फिर update करके दोबारा tuple बना सकते हैं।

1. Convert Tuple to List and Update
students = ("Abhishek", "Chaitanya", "Naincy")
student_list = list(students)
student_list[1] = "Aryan"
students = tuple(student_list)
print(students)
Output:
("Abhishek", "Aryan", "Naincy")
2. Add Item to Tuple
students = ("Abhishek", "Naincy")
students = students + ("Jayati",)
print(students)
Output:
("Abhishek", "Naincy", "Jayati")
3. Remove Item from Tuple
students = ("Abhishek", "Naincy", "Aryan")
student_list = list(students)
student_list.remove("Naincy")
students = tuple(student_list)
print(students)
Output:
("Abhishek", "Aryan")