Python List remove Function

The built-in Python list remove() function is used to remove the first occurrence of a user-specified item from an existing list. It takes one argument and searches for it inside a list. If the element is found, it stops searching and deletes that item. If the item is not available in the list, it returns a ValueError.

If you know the item that you want to delete from a list, use the Python list remove() function, and it does the work. If there are multiple occurrences (duplicates) of an item, the remove() function only removes the first occurrence and ignores the other elements.

Python list remove() syntax

The syntax of the remove() function to delete the first matching item from a list is:

listName.remove(Item)

Here, listName is an existing list from which we want to delete an element. The Python list remove() function removes the given item (item) from the listName.

Parameter: As you can see from the above remove() method syntax, it accepts only one argument, and it is mandatory. It is the item that will be removed from the existing list. This item can be of any supporting data type, including integer, float, list, string, etc.

Return Value: The Python list remove() function deletes the given item from the existing list, and it does not return any value as output. We must use the print() function to see the list without this element.

  • If we pass the item (element) that does not exist in the listName, it will throw a ValueError.
  • Using the list remove() function without an argument leads to a TypeError.

Python list remove() function Example

We start this series of remove() function examples with a basic example. In the following example, we declared a list with four characters. Next, the list remove() function searches for the first occurrence of ‘c’  and removes the third element (‘c’ ) from a list.

As we all know, the Python list remove() function returns None, so we used the print() function to display the list after removal.

a = ['a', 'b', 'c', 'd']
a.remove('c')
print(a)
['a', 'b', 'd']

Python List remove() function on integers

Similar to the above, we can use the built-in remove() function on a list of any data type. In the example below, we declared a list of unique integrator elements. Next, we utilized the remove() function to delete 75, 45, and 10 elements from the numbers. After deleting every single item, we used the print () function to display the list to see the difference.

As we all know, the Python list remove() function accepts a single argument, so we used the remove() function multiple times to delete 75, 45, and 10.

numbers = [10, 15, 25, 45, 65, 75, 98, 225]

numbers.remove(75)
print("After Del 75 are : ", numbers)

numbers.remove(45)
print("After Del 45 are : ", numbers)

numbers.remove(10)
print("After Del 10 are : ", numbers)
After Del 75 are  :  [10, 15, 25, 45, 65, 98, 225]
After Del 45 are  :  [10, 15, 25, 65, 98, 225]
After Del 10 are  :  [15, 25, 65, 98, 225]

How to remove String list items?

If we know the element or the value inside the List, then using the Python remove() function helps to delete the element. In this example, we declared a string list. Next, we used the list remove() function to delete the existing items, such as Orange, Kiwi, and Apple.

  1. First, we passed the Orange item to the remove() function to delete it from the fruits list and display the remaining.
  2. Delete the Kiwi item from the fruits and display the remaining.
  3. Next, pass Apple to the remove() function to delete it from the fruits list.

TIP: Please refer to the List and List functions articles in the Python programming language.

fruits = ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']
fruits.remove('Orange')
print("Items after Orange = ", fruits)

fruits.remove('Kiwi')
print("Items after Kiwi = ", fruits)

fruits.remove('Apple')
print("Items after Apple = ", fruits)
Items after Orange =  ['Apple', 'Grape', 'Banana', 'Kiwi', 'Cherry']
Items after Kiwi =  ['Apple', 'Grape', 'Banana', 'Cherry']
Items after Apple =  ['Grape', 'Banana', 'Cherry']

Python remove function on mixed list items

In our previous example, we used a list of elements of the same data type to perform delete operations. However, we can use the list.remove() function to delete an item from a mixed list of items (with different data types).

To demonstrate it, we declared a list of strings, positive and negative integers, a floating-point number, and a Boolean value. After calling the remove() function, we used the print() statement to display the list to see the difference.

  • In the first statement, the Python remove() function deletes Grape items from the given list.
  • In the second, we passed a floating-point number (!5.93) to delete it from a list.
  • Within the last statement, the remove() method deletes the Boolean True value from a list.
a = ['Apple', 2, 'Grape', -12, 'Banana', 15.93, 'Kiwi', True]

a.remove('Grape')
print("After Deleting Grape: ", a)

a.remove(15.93)
print("After Deleting 15.93: ", a)

a.remove(True)
print("After Deleting True : ", a)
After Deleting Grape:  ['Apple', 2, -12, 'Banana', 15.93, 'Kiwi', True]
After Deleting 15.93:  ['Apple', 2, -12, 'Banana', 'Kiwi', True]
After Deleting True :  ['Apple', 2, -12, 'Banana', 'Kiwi']

Python remove() function to delete a non-existent list item

When working with the list remove() function, we must be careful with the passing value. The remove() function searches for the given item inside a specified list from start to end. If it does not find the passing value, it returns a ValueError, so always pass an existing item as the parameter value.

To demonstrate it, we declared a string list with three fruit items. Next, we passed ‘Orange’ as the remove() function argument. It means the remove() function has to delete the Orange fruit from the list ‘a’. However, there is no Orange inside it, so the Python list remove() function returns a ValueError.

a = ['Apple', 'Grape', 'Banana']

a.remove('Orange')
print(a)
ValueError: list.remove(x): x not in list

Returning a ValueError to the end-user is not the best approach. Therefore, we can use the If Else statement to handle the error and return a custom message. Instead of ValueError, the example below prints a message stating that Orange does not exist in a list.

a = ['Apple', 'Grape', 'Banana']

if 'Orange' in a:
a.remove('Orange')
else:
print('Orange Does not Exist')
print(a)
Orange Does not Exist
['Apple', 'Grape', 'Banana']

Otherwise, use the try block and exception to return a simple one-line ValueError message

try:
    a.remove('Orange')
except ValueError as e:
    print('Error = ', e)

Python remove() function on nested lists

When it is a normal list with different kinds of data, it is easy to pass an item to the remove() function to delete it. When it comes to a nested list, we must pass the whole list (nested) to a remove function.

The remove function searches for the sub-list inside the main list and deletes the whole list (all items in it). In the following example, we declared a list of lists (5 lists nested inside a main list). Next,

  • numbers.remove([200, 45, 65]) searches for a list with 200,45, and 65 elements and removes it.
  • Similarly, the next two statements delete the other two nested lists.
numbers = [[10, 22], [1, 3], [15, 25], [200, 45, 65], [75, 98, 225]]
 
print("Available Items : ", numbers)
 
numbers.remove([200, 45, 65])
print("Available Items after  : ", numbers)
 
numbers.remove([10, 22])
print("Available Items after  : ", numbers)
 
numbers.remove([15, 25])
print("Available Items after  : ", numbers)
Python List remove Function Example

However, when working with nested lists (lists of lists), the Python remove() function can’t delete a single item from the nested list items. For example, the above list has a range of integer numbers placed in a nested list.

If we try to remove 1 from the second nested list, the remove() function throws an error. We must use the for loop or list comprehension to iterate over the nested lists and apply the remove() function on them.

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

Python remove() function on a list with duplicates

As we mentioned at the beginning, the list remove() function removes the first occurrence of an item in a list. Once it finds the required item, it stops searching and deletes it.

To demonstrate it, we used the following example and used the remove() function to delete the Orange item from the fruits. Our Fruits list contains duplicate elements, i.e., two oranges, but this remove() method deletes only the second item. This is because once the remove() function finds Orange at the second position, it deletes and stops searching for the Orange at the last position.  

fruits = ['Apple', 'Orange', 'Grape', 'Banana', 'Orange']
fruits.remove('Orange')
print(fruits)

To remove all occurrences or duplicates, there are multiple options. The Python list remove() function is definitely not a viable option. However, we show you the remove() function to understand how it works.

In the example below, the while loop iterates over the list of items and checks whether 2 is in the list. If 2 is available, use the remove() function to delete that list item. It happens until the while loop finds 2. If it does not, it exits the loop, and the print statement returns the result.

n = [1, 2, 3, 2, 2, 4, 2, 9, 11]

while 2 in n:
n.remove(2)

print(n)

As you can see, there is no 2 inside the list.

[1, 3, 4, 9, 11]

NOTE: You can also call the Python list remove() function multiple times with the same value to delete them. It is not appropriate to do so.

A better approach is to use the list comprehension without any built-in remove() function.

n = [1, 2, 3, 2, 2, 4, 2, 9, 11]

n = [x for x in n if x != 2]
print(n)

Python remove() function to delete multiple list items

As we all know, the list remove() function accepts a single argument and deletes one value. In this example, we will show how to use it to remove multiple items. However, it is only for the understanding and for real-time use of the list comprehension or filter options.

In the example below, we declared a main list and a sub list. Our job is to use the remove() function to delete 2, 3, and 7 items from n. So, we must use the second list to delete items from the first list.

The first for loop iterate items in the second list. On each iteration, the while loop checks that the item is present in the main list. If true, the Python list remove() function deletes that item. If not, move to the next element.

n = [1, 2, 3, 4, 5, 6, 7, 8]
r = [2, 3, 7]

for i in r:
while i in n:
n.remove(i)

print(n)

The best option is without using the remove() function and utilize the list comprehension technique. For instance, the code below is the best alternative to the above example.

n = [x for x in n if x not in r]
print(n)

Without using Python remove() function (Custom Code)

There are multiple options that we can use to perform the same operation that the list remove() function does. Here, we chose the simple list slicing technique to remove the first occurrence of a list item and ignore the other occurrences.

def removefun(n, value):
for i in range(len(n)):
if n[i] == value:
n[:] = n[:i] + n[i+1:]
return
print("Value not found")

a = [11, 22, 33, 44, 33, 55]
removefun(a, 33)
print(a)
[11, 22, 44, 33, 55]

Alternatives to Python list remove() function

Other than the list remove() function, there are three main options to delete items in a list. They are the pop() function, the clear() method, and the del keyword. All three of them remove list items, but they’re not alternatives to the remove() function.

  • remove(): It searches for the user-given item and deletes the first occurrence of it from a list.
  • pop(): It removes the list item at the last position or specified index position and returns that value.
  • del keyword: It also deletes the list item at the given index position, but it does not return any value.
  • clear(): It removes all items in a list and makes the list empty.

TIP: Use clear() to remove all items and del keyword to remove a single item or a range of items. For single elements, if you know the value, use Python list remove() function, and if you know the index position, use pop().

pop() method

BY default, the pop() method removes the last item from a list. However, if we pass the index position, it removes the list item at that position. In the following example, we use the pop() function to delete the item at the second index position (3).

n = [1, 2, 3, 4, 5]
print(n.pop(2))
print(n)
3
[1, 2, 4, 5]

del keyword

Similar to the pop(), the del keyword uses the index position or range of positions to delete one or more list items. The following example deletes the list item at the 4th index position.

n = [11, 22, 33, 44, 55, 66]

del n[4]
print(n)
[11, 22, 33, 44, 66]

clear() method example

If you want to remove all the existing items in a list, the clear () method is the best option. The clear() method deletes all items in a list and returns no value. When you print after applying the clear(), it is an empty list.

n = [1, 2, 3, 4, 5]
n.clear()
print(n)
[]

NOTE: For the remaining options, I suggest referring to the Python Program for Deleting List Items article.