Python – Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list() + clear() + tuple()
The combination of above 3 functions can be used to perform this task. In this, we interconvert the tuple to list, clear it and again convert to tuple using tuple().
Python3
# Python3 code to demonstrate# Clearing a tuple# using list() + tuple() + clear()# initializing tupletest_tup = (1, 5, 3, 6, 8)# printing original tupleprint("The original tuple : " + str(test_tup))# Clearing a tuple# using list() + tuple() + clear()temp = list(test_tup)temp.clear()test_tup = tuple(temp)# print resultprint("The tuple after clearing values : " + str(test_tup)) |
The original tuple : (1, 5, 3, 6, 8) The tuple after clearing values : ()
Method #2 : Reinitialization using tuple()
Another straight forward way to perform this task is to reinitialize tuple using tuple() which will return empty tuple.
Python3
# Python3 code to demonstrate# Clearing a tuple# using Reinitialization + tuple()# initializing tupletest_tup = (1, 5, 3, 6, 8)# printing original tupleprint("The original tuple : " + str(test_tup))# Clearing a tuple# using Reinitialization + tuple()test_tup = tuple()# print resultprint("The tuple after clearing values : " + str(test_tup)) |
The original tuple : (1, 5, 3, 6, 8) The tuple after clearing values : ()
Method #3: Using * operator
This method uses the * operator to create a new tuple with zero copies of the original tuple.
Python3
# initializing tupletest_tup = (1, 5, 3, 6, 8)# printing original tupleprint("The original tuple : " + str(test_tup))# Clearing a tuple using * operatortest_tup = test_tup * 0# print resultprint("The tuple after clearing values : " + str(test_tup))#This code is contributed by Edula Vinay Kumar Reddy |
The original tuple : (1, 5, 3, 6, 8) The tuple after clearing values : ()
All of the above methods for clearing a tuple have a time complexity of O(n) and an auxiliary space of O(1) because the operations are performed on the existing tuple.
Method #4: Using slicing and concatenation
- Initialize a tuple called test_tup with values (1, 5, 3, 6, 8).
- Print the original tuple using the print() function and the string concatenation operation +. The str() function is used to convert the tuple to a string.
- Use slicing and concatenation to clear the tuple. Slicing is used to get an empty tuple, and concatenation is used to combine it with the original tuple. The resulting tuple is assigned back to the variable test_tup.
- Print the resulting tuple using the print() function and the string concatenation operation +. The str() function is used to convert the tuple to a string.
Python3
# initializing tupletest_tup = (1, 5, 3, 6, 8)# printing original tupleprint("The original tuple : " + str(test_tup))# Clearing a tuple using slicing and concatenationtest_tup = test_tup[0:0] + ()# print resultprint("The tuple after clearing values : " + str(test_tup)) |
The original tuple : (1, 5, 3, 6, 8) The tuple after clearing values : ()
Time complexity: O(n) where n is the length of the tuple, because slicing takes O(n) time.
Auxiliary space: O(1), because no extra memory is used.
METHOD 5:Using the del keyword
APPROACH:
In this approach, we can simply delete the original tuple using the del keyword, which clears all the values of the tuple.
ALGORITHM:
1. Assign the tuple to a variable.
2. Use the del keyword to delete the tuple.
3. Print the original tuple and an empty tuple to show that all values have been cleared.
Python3
# Inputtup = (1, 5, 3, 6, 8)# Delete the original tuple using the del keyworddel tup# Outputprint("The original tuple:", (1, 5, 3, 6, 8))print("The tuple after clearing values:", ()) |
The original tuple: (1, 5, 3, 6, 8) The tuple after clearing values: ()
Time Complexity: O(1)
Space Complexity: O(1)
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

