Home » Python

Difference between del, remove and pop functions of a list in Python

Python list | del, remove and pop functions: Here, we are going to learn about the del, remove and pop function of a list in python and what are the differences between them?
Submitted by Yash Khandelwal, on March 29, 2019

1) Python del function

del is nothing but "delete". del is a keyword which basically goes on the position given by the user in del(position) and deletes that element and also changes the positions of all the other elements as it is now not the part of the list.

One import thing in delete is that it takes the argument in it is id i.e. not the whole data which is to be deleted only the location of the data.

2) Python remove function

remove is nothing only the searching of the first occurrence of the element and removes that element.

Note: "remove" is slower than the "del" as it has to search the element which makes it slow.

3) Python pop function

The pop() only takes the single argument i.e the index and removes the element from there without affecting any others position. If we pass the index which was not in the range of the given list then it through the error i.e. "IndexError: pop index out of range".

It is not necessary to pass the argument in the pop function if not passed it takes it -1 by itself and remove the element from the last and delete that location from the list.

Python example for del, remove and pop functions in the list

l=[1,2,3,4,6,5,6,7,8,6] #list 
# del deletes the 4th position element i.e 6  
#new list after deletion
print('After deletion:',l)   

#removes the value 6 from list
# new list after removing
print('After removing:',l)    

#pop of the element at location 1 
# new list after pop
print('After pop:',l)     

#pop of the element from the last of the list
# new list after pop
print('After pop:',l)     

#pop of the elements from the 
# new list after pop
print('After pop:',l)


After deletion: [1, 2, 3, 4, 5, 6, 7, 8, 6]
After removing: [1, 2, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 8, 6]
After pop: [1, 3, 4, 5, 8]

Explanation of the code:

    In the above code,
    deletes the 4th position element  i.e. 6 of the list,
    and also change the position/location of all other further elements.
    Removes the element 6 from the list. 
    And, while using pop in list
    Pops off  the first element of the list .
    Pops off the 3rd element from the last 
    that means negative value means from last
    l.pop( )
    If not given any argument by default take that -1.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.