Sometimes, while working with tuples, being immutable, can offer a lot of confusion regarding its working. One of the questions that can pop from mind is, that Are objects inside tuples mutable?. The answer to this is Yes. Let’s discuss certain ways in which this can be achieved.
Method #1 : Using Access methods
This is one of the way in which edit inside objects of tuples can be performed. This occurs similar to any other container and inplace using list access method.
# Python3 code to demonstrate working of # Edit objects inside tuple # Using Access Methods # initializing tuple test_tuple = (1, [5, 6, 4], 9, 10) # printing original tuple print("The original tuple : " + str(test_tuple)) # Edit objects inside tuple # Using Access Methods test_tuple[1][2] = 14 # printing result print("The modified tuple : " + str(test_tuple)) |
The original tuple : (1, [5, 6, 4], 9, 10) The modified tuple : (1, [5, 6, 14], 9, 10)
Method #2 : Using pop() + index()
The combination of above functionalities can also be used to solve this problem. In this, we perform task of removak using pop() and add element at particular index using index().
# Python3 code to demonstrate working of # Edit objects inside tuple # Using pop() + index() # initializing tuple test_tuple = (1, [5, 6, 4], 9, 10) # printing original tuple print("The original tuple : " + str(test_tuple)) # Edit objects inside tuple # Using pop() + index() test_tuple[1].pop(2) test_tuple[1].insert(2, 14) # printing result print("The modified tuple : " + str(test_tuple)) |
The original tuple : (1, [5, 6, 4], 9, 10) The modified tuple : (1, [5, 6, 14], 9, 10)
Recommended Posts:
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- PyQt5 - Setting line edit to the ComboBox
- PyQt5 - How to get line edit of ComboBox
- PyQt5 - Setting font to the item in line edit box of ComboBox
- PyQt5 - Set skin to line edit part of combobox
- PyQt5 - Set skin to line edit part of non-editable combobox
- PyQt5 Combo Box – Different border width to line edit part
- PyQt5 QComboBox – Change border style of line edit part
- PyQt5 QSpinbox - How to add line edit object to it
- PyQt5 QSpinbox - Accessing the line edit object of it
- PyQt5 QSpinBox - Adding background color to the line edit part
- PyQt5 QCalendarWidget - Enabling/Disabling Date Edit Pop-Up
- PyQt5 QCalendarWidget - Checking if Date Edit Pop-Up is enabled or disabled
- PyQt5 QCalendarWidget - Setting Date Edit (Pop-Up) Accept Delay Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

