Python – Clearing a tuple
Last Updated :
18 May, 2023
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
test_tup = ( 1 , 5 , 3 , 6 , 8 )
print ( "The original tuple : " + str (test_tup))
temp = list (test_tup)
temp.clear()
test_tup = tuple (temp)
print ( "The tuple after clearing values : " + str (test_tup))
|
Output :
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
test_tup = ( 1 , 5 , 3 , 6 , 8 )
print ( "The original tuple : " + str (test_tup))
test_tup = tuple ()
print ( "The tuple after clearing values : " + str (test_tup))
|
Output :
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
test_tup = ( 1 , 5 , 3 , 6 , 8 )
print ( "The original tuple : " + str (test_tup))
test_tup = test_tup * 0
print ( "The tuple after clearing values : " + str (test_tup))
|
Output
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
test_tup = ( 1 , 5 , 3 , 6 , 8 )
print ( "The original tuple : " + str (test_tup))
test_tup = test_tup[ 0 : 0 ] + ()
print ( "The tuple after clearing values : " + str (test_tup))
|
Output
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
tup = ( 1 , 5 , 3 , 6 , 8 )
del tup
print ( "The original tuple:" , ( 1 , 5 , 3 , 6 , 8 ))
print ( "The tuple after clearing values:" , ())
|
Output
The original tuple: (1, 5, 3, 6, 8)
The tuple after clearing values: ()
Time Complexity: O(1)
Space Complexity: O(1)
Please Login to comment...