How Can We Help?
Introduction:
In this tutorial, we are going to explain how to use Python tuple delete function with basic syntax and many examples for better understanding.
Python tuple delete is used to perform deletion operation, del () function is used in tuple. Using this function, we can delete entire tuple or individual tuple element or all element of a tuple.
Syntax:
del (<tuple_variable_name>)
Input Parameters:
- tuple_variable_name : where tuple is a tuple variable which we need to delete it.
Example (1): Delete single element of tuple
#delete single element
number=(1,2.5,1.2,3)
del (number[0])
print(number)
Output of example 1
Traceback (most recent call last):
File “”, line 1, in
del (number[0])
TypeError: ‘tuple’ object doesn’t support item deletion
In the above Example , we are deleting element [0] of tuple variable ‘number’ by del (number[0]) so in output ,we got type error i.e. ‘tuple’ object doesn’t support item deletion.
Example (2): Delete multiple element of tuple
#delete multiple element
alphabet=('a','b','c','d','e')
del(alphabet[1:3])
print(alphabet[1:3])
Output of example 2
Traceback (most recent call last):
File “”, line 1, in
del(alphabet[1:3])
TypeError: ‘tuple’ object doesn’t support item deletion
In the above Example , we are deleting multiple element as 1st ,2nd and 3rd index in alphabet by ‘del(alphabet[1:3])’ so output display the value of 4th and 5th index only as [‘d’, ‘e’].
Example (3): Delete the whole tuple
mytuple=(1,'Python',5.2,'language!')
del (mytuple)
# after deleting tuple
print(mytuple)
Output of example 3
Traceback (most recent call last):
File “”, line 1, in
print(mytuple)
NameError: name ‘mytuple’ is not defined
In the above Example , we are deleting tuple variable ‘mytuple’ by del (mytuple) so, we received the output as error i.e. name ‘mytuple’ is not defined because ‘mytuple’ is already deleted in the previous step.
In this tutorial, you have learned how to use python tuple delete.
If you have an inquiry or doubt don’t hesitate to leave them in comment. we are waiting your feedback.But remember to understand the concept very well, you need to practice more.
Hopefully, it was clear and concise.
Similar Python tuple delete Function:
- Tuples: is a form of data collection where different type of data can be stored and separated by commas(,).
- min(tuple) : It returns a minimum value of tuple.
- max(tuple) : It returns a maximum value of tuple.
- len(tuple) : It returns type of tuple , if passed variable is tuple.
- tuple(seq) : It converts a sequence/string into tuple.
- del(tuple) : It deletes whole tuple or individual tuple element or all elements of tuple.
Python tutorials list:
- Python tutorials list : access all python tutorials.