In this Python tutorial, we will learn the three different techniques to remove or delete elements from the list. These are del, remove, and pop. Moreover, we will also know the difference between del, remove, and pop on Python lists. In Python, we don't have arrays. Thus, we use the list data structure as an alternative to arrays to store elements or items in sequential order. Inserting and deleting elements from a list is easy, and there are many methods provided by the list itself for the insertion and removal of elements between the list.
Del, Remove, Clear, and Pop in Python Lists
Python del Keyword with a List
is a Python reserve word that is used to delete the complete object or variable from the program. We can use the
keyword to delete the individual list element or the complete list itself.
del list_name[index] # delete specific element del list_name #delete the complete list
>>> my_list = [1,2,3,4,5,6,7] >>> del my_list #delete 1 from the list >>> my_list [2, 3, 4, 5, 6, 7] >>> del my_list #delete the complete list >>> my_list Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'my_list' is not defined
Python List remove() Method
Python list method
that is used to remove a specific value from the list. The remove method accepts the element value as a parameter and deletes the first occurrence of the value from the list. The remove method returns a None value. If the specified value does not exist in the list, then the remove method throws a
>>> my_list = [1,1,2,2,3,3] >>> my_list.remove(2) #remove the first 2 from the list >>> my_list [1, 1, 2, 3, 3] >>> my_list.remove(12) #try to remove non exist element Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
Python List pop Method
By default, the
method is used to remove the last element from the list, but it also accepts an optional parameter that represents the index number that is supposed to pop out or delete from the list. The pop() method also returns the value that it popped out or deleted from the list.
>>> my_list = [1,1,2,2,3,3] >>> my_list.pop() 3 >>> my_list [1, 1, 2, 2, 3] >>> my_list.pop(0) #remove the first element 1 >>> my_list [1, 2, 2, 3]
Python List clear() Method
If you want to delete all the elements from the list, then instead of using the
method in for loop, we can use the list clear() method that will delete all the elements from the list.
>>> my_list = [1,1,2,2,3,3] >>> my_list.clear() >>> my_list 
Del, remove, clear, and pop on Python Lists: A Comparison Table
|Type||It is a Python keyword.||remove() is a list method.||It is a list method.||clear() is a list method.|
|Deletes||del can delete specific elements from the list or the complete list.||It can delete a specific value from the list.||pop() can delete specific values from the list using its index.||The clear() method can delete all the elements from the list, making the list empty.|
|Returns||It returns None.||The remove() method returns None.||It returns the deleted value.||It returns None.|
The del keyword can delete an element or an entire list, while the remove method deletes only a specific value from the list. The pop() method is generally used to remove the last element from the list, and the clear() method is used to delete all the elements from the list in one go. Unlike del, remove(), pop(), and clear() are list methods .
People are also reading:
- Del, Remove and Pop on Python Lists
- Python Keywords & Identifiers
- Input Output (I/O) and Import in Python
- Python Exception or Error
- Global Keyword in Python
- Python Continue and Break Keywords
- Pass Keyword in Python
- What is Python Used For?
- Data Science Packages for Python
- How to Slice List/Arrays and Tuples in Python?
Leave a Comment on this Post