- Home
- /
- Python Manipulating Tuples
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Manipulating Tuples
Python Manipulating Tuples
Tuples in Python are immutable, which means once a tuple is created, you can’t change, add, or remove its items directly. But don’t worry — there’s a workaround!
How to Modify a Tuple
If you really need to edit a tuple, here’s what you do:
Convert the tuple into a list.
Make changes using list operations.
Convert it back into a tuple.
Let’s see this in action:
cities = ("Lahore", "Karachi", "Islamabad", "Quetta", "Peshawar")
temp_list = list(cities) # Step 1: Convert to list
temp_list.append("Multan") # Step 2: Add an item
temp_list.pop(3) # Step 3: Remove item at index 3 ("Quetta")
temp_list[2] = "Rawalpindi" # Step 4: Change value at index 2
cities = tuple(temp_list) # Step 5: Convert back to tuple
print(cities)
Output:
('Lahore', 'Karachi', 'Rawalpindi', 'Peshawar', 'Multan')
Joining Tuples Directly
You don’t need to convert to a list if you just want to combine two tuples — simply use the +
operator.
north = ("Punjab", "KPK", "Gilgit")
south = ("Sindh", "Balochistan")
pakistan_regions = north + south
print(pakistan_regions)
Output:
('Punjab', 'KPK', 'Gilgit', 'Sindh', 'Balochistan')
Summary
Tuples are read-only once created.
To modify: Convert → Edit → Convert back.
Use
+
to join tuples without conversion.
Simple, right?
Keep experimenting and you’ll master tuples in no time — only with Learn With Arshyan.