Open In App

Python – Clearing a tuple

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 tuple
test_tup = (1, 5, 3, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Clearing a tuple
# using list() + tuple() + clear()
temp = list(test_tup)
temp.clear()
test_tup = tuple(temp)
 
# print result
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




# Python3 code to demonstrate
# Clearing a tuple
# using Reinitialization + tuple()
 
# initializing tuple
test_tup = (1, 5, 3, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Clearing a tuple
# using Reinitialization + tuple()
test_tup = tuple()
 
# print result
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




# initializing tuple
test_tup = (1, 5, 3, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Clearing a tuple using * operator
test_tup = test_tup * 0
 
# print result
print("The tuple after clearing values : " + str(test_tup))
 
#This code is contributed by Edula Vinay Kumar Reddy


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




# initializing tuple
test_tup = (1, 5, 3, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Clearing a tuple using slicing and concatenation
test_tup = test_tup[0:0] + ()
 
# print result
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




# Input
tup = (1, 5, 3, 6, 8)
 
# Delete the original tuple using the del keyword
del tup
 
# Output
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)



Previous Article
Next Article

Similar Reads

Python | Clearing list as dictionary value
Clearing a list is a common problem and solution to it has been discussed many times. But sometimes, we don't have a native list but list is a value to dictionary key. Clearing it is not as easy as clearing an original list. Let's discuss certain ways in which this can be done. Method #1: Using loop + clear() This is the most generic method in whic
6 min read
Python PRAW - Clearing the vote of a comment in Reddit
In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. We can either upvote or downvote a comment. Here we will see how to remove either the upvote or the downvote from a comment using PRAW. We will be using the clear_vote() method of the Comment class to clear the vote of a comment. cle
2 min read
PyQt5 QSpinBox – Clearing the text
In this article we will see how we can clear the text i.e value of the spin box, clearing doesn't mean resetting the value means to revert back the original value, clearing the value/text means completely erase the content of the spin box. In order to do this we will use clear() method. Syntax : spin_box.clear() Argument : It takes no argument Retu
2 min read
PyQt5 QSpinBox - Clearing the Mask
In this article we will see how we can clear the mask from the spin box, masks are used to hide part of a UI Image element. We can set mask using setMask method. In order to do this we use clearMask method. Syntax : spin_box.clearMask() Argument : It takes no argument Action Performed : It mask on the spin box Implementation steps : 1. Create a mai
2 min read
PyQt5 QCalendarWidget - Clearing Focus
In this article we will see how we can clear the focus of the QCalendarWidget if it has focus. Setting focus is basically setting focus reason there are many focus reason available like backtab, popup reason etc. Setting focus is the overloaded function. We use setFocus method to set focus to the calendar. In order to do this we will use clearFocus
1 min read
PyQt5 QDateTimeEdit – Clearing maximum QDateTime
In this article we will see how we can remove the maximum QDateTime of the QDateTimeEdit widget. QDateTime is basically a combination of QDate and QTime i.e it has both date and time. And QDateTimeEdit widget is used to show or receive the QDateTime. We can set date time to it with the help of setDateTime. By setting maximum value of it make sure t
2 min read
PyQt5 QDateTimeEdit – Clearing minimum QDateTime
In this article we will see how we can clear/remove the minimum QDateTime of the QDateTimeEdit widget. QDateTime is basically a combination of QDate and QTime i.e it has both date and time. And QDateTimeEdit widget is used to show or receive the QDateTime. We can set date time to it with the help of setDateTime. By setting minimum value of it, make
2 min read
PyQt5 QDateTimeEdit – Clearing Layout Direction Property
In this article we will see how we can remove/clear the selected layout direction of the QDateTimeEdit widget. Clearing layout direction will revert back layout direction to the default. Layout direction specifies the direction of the QDateTime, by default the arrow buttons which increment or decrement date-time are on the right-hand side although
2 min read
PYGLET – Clearing Window
In this article we will see how we can clear a window in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can be set to fill an entire screen
2 min read
PyQtGraph - Clearing the Line in Line Graph
In this article, we will see how we can clear the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A line c
3 min read
Practice Tags :
three90RightbarBannerImg