Remove all items from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove all items from a List in Python #

Use the list.clear() method to remove all items from a list, e.g. my_list.clear(). The list.clear() method removes all items from the specified list.

main.py
# ✅ Remove all items from a list (using list.clear()) my_list = ['apple', 'banana', 'kiwi'] my_list.clear() print(my_list) # 👉️ [] # ------------------------------------ # ✅ Remove all items from a list (using del) my_list = ['apple', 'banana', 'kiwi'] del my_list[:] print(my_list) # 👉️ [] # ------------------------------------ # ✅ Remove all occurrences of a value from a list my_list = ['apple', 'banana', 'apple', 'kiwi', 'apple'] # 👇️ using list comprehension new_list = [item for item in my_list if item != 'apple'] print(new_list) # 👉️ ['banana', 'kiwi'] # 👇️ using for loop for item in my_list.copy(): if item == 'apple': my_list.remove(item) print(my_list) # 👉️ ['banana', 'kiwi']

The first example uses the list.clear() method to remove all items from a list.

The list.clear method removes all items from the list the method is called on.

main.py
my_list = ['apple', 'banana', 'kiwi'] my_list.clear() print(my_list) # 👉️ []

Alternatively, you can use the del statement.

Remove all items from a List using del #

Use the del statement to remove all items from a list, e.g. del my_list[:]. The del statement will remove all items from the list.

main.py
my_list = ['apple', 'banana', 'kiwi'] del my_list[:] print(my_list) # 👉️ []

The del statement is used to remove one or more items from a list by index.

We didn't specify any indexes in the slice, so the slice represents the entire list.

main.py
my_list = ['apple', 'banana', 'kiwi'] print(my_list[:]) # 👉️ ['apple', 'banana', 'kiwi']

The syntax for list slicing is my_list[start:stop:step].

When the start, stop and step values are omitted, the slice represents the entire list.

The list.clear() method is equivalent to using del my_list[:]. However, it is a bit easier to read.

If you need to remove all occurrences of a value from a list, use a list comprehension.

main.py
my_list = ['apple', 'banana', 'apple', 'kiwi', 'apple'] new_list = [item for item in my_list if item != 'apple'] print(new_list) # 👉️ ['banana', 'kiwi']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is not equal to the string apple and return the result.

The new list won't contain any items that don't meet the condition.

The list comprehension doesn't mutate the original list, it returns a new list.

If you need to mutate the original list, use a for loop.

main.py
my_list = ['apple', 'banana', 'apple', 'kiwi', 'apple'] for item in my_list.copy(): if item == 'apple': my_list.remove(item) print(my_list) # 👉️ ['banana', 'kiwi']

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove items from a list while iterating over it.

However, we can iterate over a copy of the list and remove items from the original list.

main.py
my_list = ['apple', 'banana', 'apple', 'kiwi', 'apple'] for item in my_list.copy(): if item == 'apple': my_list.remove(item) print(my_list) # 👉️ ['banana', 'kiwi']

On each iteration, we check if the current item is equal to the string apple and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.