Guide to remove item from list Python with examples. How can you remove an item from a list in python?

From time to time in your python programs, you need to work with lists of items. One important operation you need in order to work with your list is to remove one or more items from your list. This tutorial will walk you through how to remove item from list in python.

We will cover several item removal scenarios. These scenarios are

  • Removing an item based on item value
  • Removing an item based on position
  • Removing an item that satisfies a condition
  • Removing all items in a list

Remove an item from a python list based on item value

The list remove() method helps us remove an item from a list based on its value. All we need to do is to pass the item as an argument and remove() will search the list for the first occurrence of the item, and then remove it from the list.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# call the remove method
numbers.remove(-3)

# display the result
print(f"The new list: {numbers}")

Output:

The new list: [12, 4, 0, 5, 17, -10, 0, -3]

The remove() method raises a ValueError if we attempt to remove an item that does not exist in the list.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# call the remove method
numbers.remove(20)

# display the result
print(f"The new list: {numbers}")

Output:

ValueError Traceback (most recent call last)
<ipython-input-6-6c9753200a84> in <module>
3
4 # call the remove method
----> 5 numbers.remove(20)
6
7 # display the result

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

Remove an item based on position

The remove() method removes an item when given its value. If we wanted to remove the item based on its position in the list, we need a different Python method, pop(). The pop() method takes one optional argument, the index, and removes the item from that position in the list.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# call the pop method
removed_item = numbers.pop(5)

# display the result
print(f"The removed item: {removed_item}")
print(f"The new list: {numbers}")

Output:

The removed item: 17
The new list: [12, -3, 4, 0, 5, -10, 0, -3]

Since the argument of the pop() method is optional, we do not need to specify an argument. In that case, pop() will make use of a default index value of -1. In other words, pop() will just remove the last element from the list.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# call the pop method
removed_item = numbers.pop()

# display the result
print(f"The removed item: {removed_item}")
print(f"The new list: {numbers}")

Output:

The removed item: -3
The new list: [12, -3, 4, 0, 5, 17, -10, 0]

If the index is greater or equal to the number of items in the list, pop() raises an IndexError exception.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# call the pop method
removed_item = numbers.pop(15)

# display the result
print(f"The removed item: {removed_item}")
print(f"The new list: {numbers}")

Output:

IndexError Traceback (most recent call last)
<ipython-input-13-554149676daa> in <module>
3
4 # call the pop method
----> 5 removed_item = numbers.pop(15)
6
7 # display the result

IndexError: pop index out of range

Using the del statement

The del statement in Python is used to remove objects and any references to them. del can also be used to remove items from a list. All we need to do is to specify the list and the index or the range of indexes that we want to be affected.

The following code removes a single item from a list


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# use the del statement
item_to_remove = numbers[5]
del numbers[5]

# display the result
print(f"The removed item: {item_to_remove}")
print(f"The new list: {numbers}")

Output:

The removed item: 17
The new list: [12, -3, 4, 0, 5, -10, 0, -3]

We can also use del to remove a range of items from the list. To do this, specify the range to be removed by using the slice operator.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# use the del statement
items_to_remove = numbers[3:6]
del numbers[3:6]

# display the result
print(f"The removed items: {items_to_remove}")
print(f"The new list: {numbers}")

Output:

The removed items: [0, 5, 17]
The new list: [12, -3, 4, -10, 0, -3]

Remove an item that satisfies a condition

The remove() method only removes the first instance of the item in the list. We could use a loop to remove all instances of the same item from the list.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# remove all instances of -3 from the list using a while loop
item_to_remove = -3

while item_to_remove in numbers:
    numbers.remove(item_to_remove)

# display the result
print(f"The new list: {numbers}")

Output:

The new list: [12, 4, 0, 5, 17, -10, 0]

However, List Comprehensions provide a simpler syntax for carrying out the same task. List Comprehensions can help us filter out items that do not satisfy a particular condition, while retaining list items that satisfy the condition. The following example will illustrate.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# filter out all the -3s using list comprehension syntax
new_list = [num for num in numbers if (num != -3)]

# display the result
print(f"The new list: {new_list}")

Output:

The new list: [12, 4, 0, 5, 17, -10, 0]

From the example, we saw that the list comprehension could also remove duplicates. To remove elements from a list, the elements we wanted to retain had to satisfy the given condition.

Remove all items from a list in python

If we need to empty out a list without destroying the list, we have to call the clear() method. The clear() method removes all the items from the list, leaving the list with no elements. The method does not return any values but just modifies the underlying list object.


# define the list of numbers to work with
numbers = [12, -3, 4, 0, 5, 17, -10, 0, -3]

# clear out all items in the list using clear() method
numbers.clear()

# display the result
print(f"The new list: {numbers}")

Output:

The new list: []

The output shows that the items in the list are all removed, but the list is still left.

Conclusion

In this tutorial, we discussed the different scenarios to remove items from a list in python. We can remove an item based on its value (using the remove() method), remove items based on their position, or index (using the pop() method or the del statement). Also, we can remove all occurrences of an item in the list (using a while loop with the remove() method or a List Comprehension), and finally, we saw how we can remove all items from a list (using the clear() method).

We hope the information proves useful to you in your coding exercises. Thanks for reading the tutorial!

If you are in a need of any python help or looking for someone who can help with python then feel free to drop us a message.