Differences between del, remove, & pop in Python lists

Like most other programming languages, a list in python is a mutable data structure with an ordered sequence of elements. Each element or value inside a list is known as an item. The items parallel to each index can be manipulated by using a built-in method. In this blog, we are going to elaborate on del(), remove(), and pop() methods in python lists.

All three given above, del(), remove() and pop() are pre-built python methods used to manipulate lists. These functions are used to remove elements from a list, but they perform their jobs in a bit different manner from one another.

del()

The del() function takes a list having the index number enclosed inside the square brackets ‘[ ]’ as parameter. When executed, this function goes to the passed index (argument) and deletes the item present at that index. This function doesn’t return anything.

Explanation: Coding Example

Now let’s elaborate on del() with a code example that will help us better understand this method. del() deletes the element using its index without returning any value.

example_list = [ 6, 'My', 7, 'name', 32, 'is', 52, 'John' ]
print ("List before applying del: ",example_list)
 #del() removes the element at specified index
del(example_list[3])
print ("List after applying del: ",example_list)
  • Line 1: Initializing a list containing some integers and strings.
  • Line 2: Print the list before applying any function.
  • Line 4: Deleting the item at index 3 using del() function.
  • Line 5: Printing the list after applying del() function at index 3.

Output

del method in python lists
del method in python lists

remove()

The remove() function takes a list having the actual item passed inside the parentheses ‘( )’ as a parameter. When executed, this function starts scanning the list. Once the target item is found, it simply removes that item from the list. Like the del() function, remove() doesn’t return anything.

Example

Now let’s elaborate on remove() methods and how it helps to manipulate python lists. remove() removes the element from the list based on its value.

example_list = [ 6, 27, 7, 95, 32 ]
print ("List before applying remove: ",example_list)
 #remove() removes the element from the list
example_list.remove(95)
print ("List after applying remove: ",example_list)
  • Line 1: Initializing a list containing some integers.
  • Line 2: Print the list before applying any function.
  • Line 4: Removing the item with value 95 using remove() function.
  • Line 5: Print the list after applying remove() function.

Output

remove method in python lists
remove method in python lists

pop()

The pop() function accepts a list having an index number enclosed inside the parentheses ‘( )’ as a parameter. When executed, this function goes to the passed index and deletes the item present at that index. This deleted item is returned by the pop() function.

Example

Now let’s elaborate on pop() that removes the element by index and returns the removed value.

example_list = [ 6, 'My', 7, 'name', 32, 'is', 52, 'John' ]
print ("List before applying pop: ",example_list)
# element from last index is popped into variable named 'popped'
popped=example_list.pop(-1)  
print ("List after applying pop: ",example_list)
print ("You have popped: ",popped)
  • Line 1: Initializing a python list containing some integer & string values.
  • Line 2: Print the list before applying any function.
  • Line 4: Removing the item at the last index of the list using pop() function & returning it in a variable named popped.
  • Line 5: Printing the list after applying pop() function.
  • Line 6: Printing the popped item.

Output

pop methods in python lists
pop method in python lists

The working of pop() and del() is quite similar. The only difference is their use cases del() has no return type, whereas, pop() returns whatever item was deleted from the list.

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...