What is the difference between del, remove and pop on lists in python ?

Object Oriented ProgrammingProgrammingPython

It does't matter how many lines of code you write in a program. When you want to remove or delete any elements from the Python list, you have to think about the difference between remove, del and pop in Python List and which one to use

remove : remove() removes the first matching value or object, not a specific indexing. lets say list.remove(value)

Example

list=[10,20,30,40]
list.remove(30)
print(list)

Output

[10, 20, 40]


del : del removes the item at a specific index. lets say del list[index]

Example

list = [10,20,30,40,55]
del list[1]
print(list)

Output

[10, 30, 40, 55]


pop : pop removes the item at a specific index and returns it. lets say list.pop(index)

Example

list = [100, 300, 400,550]
list.pop(1)
print(list)

Output

[100, 400, 550]
raja
Sri
Published on 26-Jun-2019 15:25:00
Advertisements