Difference between remove(), pop() , clear() and del in Python List
Difference between remove() , pop() ,delete and clear() in Python List.
In this article, we will learn about some methods of python list, in which we are always confused as by names it seems that all are the same but it is not the same there are very big differences between these methods. so let's start by first knowing about the work and usage of each and after that, we will talk about the differences among all.
- remove():
remove() is an inbuilt function in Python programming language that removes a given object from the list. It removes the object from the list. It does not return any value.
Note: It removes the first occurrence of the object from the list. It does not deal with the index.
Syntax of remove() method:
list.remove(index)
Code:
#python 3 program to demonstrate the use of remove () method.
#removes "banana" from list1
list1 = ["apple", "banana", "cherry"]
list1.remove("banana")
print(list1)
#removes first occurrence of 6 from list2
list2 = [1,3,6,9,6,8,4,6]
list2.remove(6)
print(list2)
#removes() returns ValueError when passed object is not present in the list.
list2 = [1,3,6,9,6,8,4,6]
list2.remove(5)
print(list2)
Output:
Sometimes we need to perform the operation of removing list from the list of lists.let's discuss the way in which this can be performed.
Code:
#removes list from list of lists
test_list = [[1, 3], 40, 9, 6, 8, 4, 60,]
test_list.remove([1,3])
print(test_list)
Output:
2. del
To remove items by index or slice we can use the del method in python .you can also remove elements from a list with del statements.
Note: It removes the specified index element.
Syntax of del :
del list[index] or del list
Code:
#python 3 program to demonstrate the use of del() method.
#this deletes the 0th element from the list.
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print(thislist)
del thislist[0]
print(thislist)
#output:
#[78, 'a', 89, 'b', 'z', 1, 20, 3, 4, 'm']
#['a', 89, 'b', 'z', 1, 20, 3, 4, 'm']
#this deletes the -1th element from the list.
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print(thislist)
del thislist[-1]
print(thislist)
#output:
#[78, 'a', 89, 'b', 'z', 1, 20, 3, 4, 'm']
#[78, 'a', 89, 'b', 'z', 1, 20, 3, 4]
#this deletes the element from index 1 to 5(5 excluded) from the list.
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print(thislist)
del thislist[1:5]
print(thislist)
#output:
#[78, 'a', 89, 'b', 'z', 1, 20, 3, 4, 'm']
#[78, 1, 20, 3, 4, 'm']
#this delete the entire list
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print(thislist)
del thislist
print(thislist)
#ouput:
#Traceback (most recent call last):
# File "C:/Users/Mousmi/PycharmProjects/HelloWorld/del.py", line 7, in <module>
# print(thislist)
#NameError: name 'thislist' is not defined
#[78, 'a', 89, 'b', 'z', 1, 20, 3, 4, 'm']
here we can see that before using the del keyword with the list name, the list was existing but after using the del keyword, Pychram is showing an error which is a name error that shows that the previously existing list is deleted now.
3.clear(): clear() method in python is used to empty the entire list. this can be used if someone wants an empty list for any other purpose.
Syntax of clear() method:
list.clear()
Code:
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print('thislist before clear:', thislist)
thislist.clear()
print('thislist after clear:', thislist)
Output:
4. pop() method: The pop() method removes the item at the given index from the list and returns the removed item.
Syntax of pop() method:
list.pop(index)
pop() parameter:
- It takes a single argument(index)
- If the argument is not passed then by default it removes the last element that is a list[-1]
- If the index passed to the method is not in range then it throws Index Error: pop index out of range exception.
Code:
# this removes the element on index -2 from the list
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ] print(thislist.pop(-2))
#output:
#4
# this removes the last element from the list because no argument is passed so
#it will pop the last element from the list
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print(thislist.pop())
#output:
#m
# If we provide an argument which is not in the present in the list then it throws an #error IndexError i.e pop index out of range.
thislist = [78, "a", 89, "b", "z" , 1, 20, 3, 4, "m" ]
print( thislist.pop(-12))
#output:
#Traceback (most recent call last):
# File "C:/Users/Mousmi/PycharmProjects/HelloWorld/clear().py", line 4, in <module>
# print( thislist.pop(-12))
#IndexError: pop index out of range
I hope from these code examples with the output snippet, the difference between the remove(), del, clear() and pop() is clear.
let's summarise what we learned from the above discussions.
Methods ---> |
remove() |
delete() |
clear() |
pop() |
Syntax
|
List.remove(index) |
del list[index] or del list |
list.clear() |
list.pop(index) Or list.pop() |
Parameters |
Single argument |
Single index argument Index can be positive ,negative and slice of index can also be provided |
Does not accept Any argument. |
Single index argument Index can be positive , Negative but slice of Index not acceptable. |
By Default |
Throws error if argument passed doesn’t match. |
It removes the entire List if the list name is Provided without any Argument. |
If any argument is not passed then it pops the last element. |
|
Usage |
Removes the first matching element or object. |
To delete an element Or list. |
To empty the entire list. |
To remove an element at specified index. |
Deals with Index |
No |
Yes |
No |
Yes |
Returns |
Object or element |
Does not return anything |
Does not return Any value |
Returns the deleted element |
I hope, you like this article if you find it useful then share this article with your friends.
with
Best regards, Happy Learning 🙂