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 finctions 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 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 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 : ()
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.