How to Remove an Item From a List in Python (remove, pop, clear, del)

In Python, we use list methods remove(), pop(), and clear() to remove an items (elements) from a list. Moreover, you can delete items using del statements by specifying a position or range with an index or slice. Python remove(), pop(), and clear() methods are used to remove items from a list. All these methods are discussed in this article. So let’s get started.

  1. Remove an item by value: remove()
  2. Remove all items: clear()
  3. Remove an item by index and get its value: pop()
  4. Remove items by index: del statement

1. Remove an item by value: remove()

The remove() method removes the first matching element, which is the specified value as an argument from the list.

The syntax of the remove() method is:

list.remove(element)

The remove() method accepts a single element as an argument and removes it from the list. If the element doesn’t exist in a list, it will throw ValueError: list.remove(x): x not in list exception.

Example: Remove element from the list – remove()

If the list contains more than one matching elements, the remove() method only removes the first matching element.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

l.remove('Penguin')
print(l)
# ['Anteater', 'Gopher', 'Penguin', 'Panda']

Example: Deleting a nonexistent element raises an error

l.remove('Rabbit')
# ValueError: list.remove(x): x not in list

2. Remove all items: clear()

The clear() method removes all elements from the list.

The syntax of the clear() method is:

list.clear()

The clear() method doesn’t take any parameters.

Example: Remove all items from the list – clear()

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

l.clear()
print(l)
# []

3. Remove an item by index and get its value: pop()

The pop() method removes the item at the given position (index) from the list and returns the removed item.

The syntax of the pop() method is:

list.pop(index)

The pop() method accepts a single argument (item index). If not passed, the default index -1 is passed as an argument (last item of the list). If the index passed to the method is not in the list range, it will throw IndexError: pop index out of range exception.

Example: Remove an item by index and get it value – pop()

An index in Python starts from 0, not 1. So if you have to pop the 3rd item, you need to pass 2 to the pop() method.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

print(l.pop(2))
# Gopher
print(l)
# ['Anteater', 'Penguin', 'Penguin', 'Panda']

print(l.pop(3))
# Panda
print(l)
# ['Anteater', 'Penguin', 'Penguin']

Example 2: pop() with a negative index

You can use negative values to specify the position from the end of the list.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

print(l.pop(-2))
# Penguin

print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Panda']

Example 3: pop() without an index

If the argument is not passed, the last item is removed.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

print(l.pop())
# Panda

print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin']

4. Remove items by index: del statement

You can remove elements from a list with del statements. Specify the item to be deleted by index. The first index is 0, and the last index is -1.

The syntax of the del() statement is:

del name

Here, del is a Python keyword. And, a name can be lists or dictionaries.

Example: Remove items by index: del statement

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

del l[2]
print(l)
# ['Anteater', 'Penguin', 'Penguin', 'Panda']

Example: Remove items, slices from a list

You can delete multiple elements with a slice.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

del l[2:4]
print(l)
# ['Anteater', 'Penguin', 'Panda']

Example: Remove all items with a slices

You can delete all items by specifying the entire range.

l = ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']
print(l)
# ['Anteater', 'Penguin', 'Gopher', 'Penguin', 'Panda']

del l[:]
print(l)
# []

We hope you have found this article helpful for getting help with Python homework assignments. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other Python related articles.

If you like our article, please consider buying a coffee for us.
Thanks for your support!

Support us on Buy me a coffee! Buy me a coffee!



Join the Discussion.