How Can We Help?
Introduction:
In this tutorial, we are going to explain how to use Python list delete function with basic syntax and many examples for better understanding.
Python list delete / del () function is used to perform deletion operation. Using this function, we can delete entire list or individual list element or all element of a list.
Syntax:
del (<list_variable_name>) del(<list_variable_name>[index]) del(<list_variable_name>[start position : end position])
Input Parameters:
- list_variable_name : where list is a list variable whose type needs to be found.
Delete single element of list Example (1)
#delete single element num=[1,2,3] del(num[0]) print(num)
Output of example 1
[2, 3]
In the above Example , we are deleting single element as 0th index in num variable by ‘ del(num[0])’ so output display the value of 1st and 2nd index only as[2, 3].
Delete multiple element of list Example (2)
#delete multiple element alphabet=['a','b','c','d','e'] del(alphabet[1:3]) print(alphabet[1:3])
Output of example 2
[‘d’, ‘e’]
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’].
Delete whole list Example (3)
#delete whole list mylist=[1,'Python',5.2,'language!'] del (mylist) print(mylist)
Output of example 3
Traceback (most recent call last):
File “”, line 1, in
mylist=[1,’Python’,5.2,’language!’];del (mylist);print(mylist)
NameError: name ‘mylist’ is not defined
In the above Example , we are deleting whole list by del (mylist) so, output display error as name ‘mylist’ is not defined because ‘mylist’ is already deleted in the previous step.
In this tutorial, you have learned how to use python list type.
Hopefully, it was clear and concise.
If you have a inquiry or doubt don’t hesitate to leave them in comment. we are waiting your feedback as well.
Similar Python list delete Function:
- lists : is a form of data collection where different type of data can be stored and separated by commas(,).
- min(list) : It returns a minimum value of a list.
- max(list) : It returns a maximum value of a list.
- len(list) : It returns type of list , if passed variable is a list.
- list(seq) : It converts a sequence/string into a list.
- type(list) : It deletes whole list or individual list element or all element of a list.
Python tutorials list:
- Python tutorials list : access all python tutorials.