Python List Remove, Pop and Clear

In python, remove(), pop(), and clear() are list functions used to remove elements from a list. These functions have different functionalities and have their own specific use cases. In this article, we’ll look at these methods, their syntax, and their differences with each other.

Before we proceed, here’s a quick refresher on python lists – Lists are used to store an ordered collection of items. These items can be any type of object from numbers to strings or even another list. This makes lists are one of the most versatile data structures in python to store a collection of objects. For more, check out our guide on lists and other data structures in python.

Lists are quite useful when it comes to storing sequences or collections and are frequently used in python. It may happen that you require to remove elements from a list. To do so, list functions remove(), pop(), and clear() are commonly used.

remove() is a list function in python used to remove an element by value. It removes the first occurrence of the value from the list. The following is the syntax:

sample_list.remove(x)

Here, sample_list is the list to remove the element from and x is the element to be removed.

The remove() function does not return any value (It returns None)

If the element is not present in the list, a ValueError gets raised on using the function.

Example 1: Removing an element from a list

# remove an element by value
ls1 = ['a', 'b', 'c', 'd']
ls2 = ['a', 'b', 'c', 'd', 'b', 'e']

# remove 'b' from ls1
print("For list ls1 -")
print("Original list:", ls1)
ls1.remove('b')
print("After removing b:", ls1)

# remove 'b' from ls2
print("For list ls2 -")
print("Original list:", ls2)
ls2.remove('b')
print("After removing b:", ls2)

Output:

For list ls1 -
Original list: ['a', 'b', 'c', 'd']
After removing b: ['a', 'c', 'd']
For list ls2 -
Original list: ['a', 'b', 'c', 'd', 'b', 'e']
After removing b: ['a', 'c', 'd', 'b', 'e']

In the above example, we remove the element 'b' from the lists ls1 and ls2. In ls1 since ‘b’ only occurs once, it gets removed from the list altogether. But in ls2 we have the element ‘b’ at two instances, here, the first occurrence of the element ‘b’ gets removed on using the remove() function.

Example 2: Removing an element not present in the list

# remove an element by value
ls = ['a', 'b', 'c', 'd']
# remove 'e' from ls
print("Original list:", ls)
ls.remove('e')
print("After removing e:", ls)

Output:

Original list: ['a', 'b', 'c', 'd']
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-4952bff59c13> in <module>
      3 # remove 'e' from ls
      4 print("Original list:", ls)
----> 5 ls.remove('e')
      6 print("After removing e:", ls)

ValueError: list.remove(x): x not in list

In the above example, we get a ValueError on removing the element 'e' which is not present in the list.

pop() is a list function used to remove a value based on its index. The following is the syntax.

sample_list.pop(i)

Here, sample_list is the list to remove the element from and i is the index of the element to be removed. If the index is not provided, it removes the last element from the list.

The pop() function returns the element removed (or “popped out”)

Example 1: Removing an element using index

# remove an element by index
ls = ['a', 'b', 'c', 'd']
# remove the 3rd element
print("Original list:", ls)
popped_element = ls.pop(2)
print("After removing 3rd element:", ls)
print("The removed element:", popped_element)

Output:

Original list: ['a', 'b', 'c', 'd']
After removing 3rd element: ['a', 'b', 'd']
The removed element: c

In the above example, we remove the third element from the list ls by passing the index 2 as argument to the pop() function. Also, note that unlike the remove() function, the pop function returns the element removed.

Example 2: Using pop() without index

# remove an element by index
ls = ['a', 'b', 'c', 'd']
# remove the last element
print("Original list:", ls)
popped_element = ls.pop()
print("After removing the last element:", ls)
print("The removed element:", popped_element)

Output:

Original list: ['a', 'b', 'c', 'd']
After removing the last element: ['a', 'b', 'c']
The removed element: d

In the above example, we do not pass an argument to the pop() function. In such a case, it removes the last element from the list.

clear() is a list function used to remove all the elements from a list. The following is the syntax:

sample_list.clear()

Here, sample_list is the list to be cleared. It does not take any arguments.

The clear() function does not return any value. (It returns None)

Example: Remove all the items from a list

# clear the list
ls = ['a', 'b', 'c', 'd']
# remove all the list items
print("Original list:", ls)
ls.clear()
print("After clearing the list:", ls)

Output:

Original list: ['a', 'b', 'c', 'd']
After clearing the list: []

In the above example, the clear() function is used to remove all the items from the list ls.

For more on list functions refer to the python docs.

Alternatively, you can use the del statement in python to remove elements from a list. Using del you can not only remove individual elements but also ranges.

Example 1: Remove an item using the del statement

# remove an element using del
ls = ['a', 'b', 'c', 'd']
# remove the 3rd element
print("Original list:", ls)
del ls[2]
print("After removing 3rd element:", ls)

Output:

Original list: ['a', 'b', 'c', 'd']
After removing 3rd element: ['a', 'b', 'd']

In the above example, the del statement is used to remove the third element from the list ls

Example 2: Removing a range of elements from a list

# remove an element using del
ls = ['a', 'b', 'c', 'd']
# remove the 3rd element
print("Original list:", ls)
del ls[1:3]
print("After removing elements:", ls)

Output:

Original list: ['a', 'b', 'c', 'd']
After removing elements: ['a', 'd']

In the above example, we remove a range of elements using a slice 1:3 on the list.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.