Python Remove from List: Full Guide + Examples

Know how there’s always that one item that you really wish you could just get rid of? That expired coupon in your wallet, that duplicate song on your favorite playlist, that one name with a typo in your address book. You need a quick, easy way to toss out the bad egg without abandoning the whole carton.
Python lists are no exception. You’ll often encounter one—or several!—list items that you’d prefer to ditch. In this article, we’ll explain multiple methods for removing items from a Python list. We’ll compare the techniques and do a brief discussion about computational complexity when weeding elements out of your lists. Whether you know the item’s value, the item’s position, or just the condition it meets, there's a Pythonic way to get it out.
Why Remove an Item from a List in Python
Lists are mutable Python collections. That means you can add or remove items from lists after you create them. You may encounter duplicate, outdated, or simply unwanted items, and luckily, you can remove list elements at any point in your Python program. For a few real-world examples, consider a user removing products from their online shopping cart, a worker checking items off her to-do list, or a pollster filtering likely voters based on age.
When it comes time to remove items from a list, you have several different techniques to choose from. Your choice will often come down to what you know about the item and how many elements you need to delete. You may discard items based on their value, their index, or their properties (e.g., age >= 18). Furthermore, you can get rid of one element at a time or many items all at once. Just be sure to pick the right removal method for the job.
Overview of List Methods and Statements
Like many concepts, there’s more than one way to remove an item from a list in Python. From removing by value to wiping out the entire list contents, we’re breaking down each option so you can choose the right one for your use case.
.remove(): Remove an Item by Value
The syntax of the .remove() method looks like this: list.remove(value), where list is the list to operate on and value is the value of the item you’d like to remove. This method searches your list from the beginning and takes out only the first occurrence of value. The change happens in place, which means the method directly modifies list instead of creating a new list. Additionally, its return value is None, not an updated copy of list.
This example illustrates using .remove(). The method removes only the first match of "cat". Notice how other instances remain in the list:
If Python can’t find an instance of the value you’re searching for, it will raise a ValueError, so be sure to plan for that exception or include an optional membership check with the in operator if you ever expect the list to not contain the specified value.
.pop(): Remove an Element by Index
If you know the index of the item you’d like to discard instead of its value, switch to .pop(). The syntax for .pop() follows: list.pop(index). The .pop() method removes whichever element is currently at that specific index. Just keep in mind that Python does zero-based indexing; the first element is in position 0.
Here’s code to remove an item in position 3. Like the .remove() method, .pop() modifies the list in place, but unlike .remove(), it returns the removed value:
Note that index is an optional argument. Without it, .pop() removes and returns the last element in your list. The .pop() method takes negative index numbers as well and then counts from the end of your list. For example, list.pop(-2) gives you the second-to-last item. If you happen to pass an out-of-bounds, invalid index, Python raises an IndexError.
del Statement: Deleting by Index Range with the del Keyword
The del keyword begins a Python statement that can delete single list items in a given position (del list[index]) or delete items that fall within a given index range (del list[start:stop]). Like other list slicing techniques, del includes the start index but excludes the stop index. It also deletes slices of your list in place but does not return the removed elements.
In the following code, Python removes items with indexes 2 through 4 but keeps "Fran" at index 5:
Note that passing invalid index positions when slicing with the del statement does not raise an IndexError. Python adjusts the slice bounds to fit within the list’s valid range.
Like other list slicing in Python, you may omit the start and/or stop indexes to delete items from the beginning or through the end of the list, respectively. You can include negative indices to count from the end of the list, and you can even include step values in your slices to delete every few items:
You may also use a del statement on the list itself to delete the entire list object.
.clear(): Wiping the Whole List
If you’re ready for a nuclear option, you can clear out the entire contents of a list: list.clear(). Be very careful when implementing .clear(). This method removes all the elements of your list in place and leaves an empty list in its wake.
Here it is in action:
.clear() returns None and works the same if your list has two elements or two million. Unlike running a del statement on a list (e.g., del costs), the list object remains after applying .clear(). You’re most likely to reach for .clear() when you need to empty out the list contents but keep the list object itself.
Removing All Occurrences of a Value
The list removal techniques presented so far focused on getting rid of one element, elements within a given range, or the entire contents of a list. We now turn to removing all occurrences of a specified value from a list. Again, you have a few different options.
List Comprehensions
You may choose to use a list comprehension to remove all instances of a value. This quick example shows how we can get rid of "cat" throughout the entire list:
Notice that this technique does not operate in place. It creates a brand-new list, stored in the pets_no_cats list variable, and your original list remains untouched.
You may also remove all occurrences of two or more values by using not in and a set of values to discard:
filter()
You may choose the filter() function to keep all elements where the input function evaluates as truthy. The syntax for it is: filter(function, iterable), where the function may be a lambda, a built-in, or your own user-defined function and the iterable in our case is your list.
Here’s how you can remove all instances of "cat" with filter() and a lambda function:
The filter() function returns an iterator, so you’ll need to convert it by wrapping the result in list() if you want pets_no_cats to be a list variable. Like the list comprehension, your original list remains unchanged.
while Loop
If you’d prefer to stick with a method like .remove(), you may choose to get rid of each occurrence individually within a while loop.
This code continues running until the .remove() method deletes all instances of "cat". The while loop condition ensures that .remove() only runs if "cat" exists in the list, avoiding a ValueError:
Because the while loop relies on .remove(), this technique modifies the original list in place. While it’s quite clear what this method does, it’s not the most efficient to delete multiple items since Python must potentially search through the entire list each time it checks the condition and each time it calls .remove().
Removing Elements by Condition
You can extend the methods for removing all occurrences to filter a list based on a condition instead of a specific value. Your condition could be a numerical threshold, a string property, or an object attribute, but once again, you can turn to list comprehensions and filter().
Say you’d like to select passing grades from a list of student scores. If a passing grade in your course is 60 or above, this code helps you filter your list:
Like removing all occurrences of a value, you’ll create a new list of filtered items via the list comprehension. Your original list remains untouched.
You can also use the filter() function to remove multiple items based on a condition. This code uses filter() to create passing_scores:
filter() uses lazy evaluation, meaning it processes elements as needed rather than immediately creating a new collection. Don’t forget to wrap its return value in list() to view the filtered result as a list in Python.
Removing Duplicate List Items
You’ll need a new approach if your goal is to keep a single occurrence and remove the extras. You once again have several different options, but we’ll keep our explanation focused on sets and dictionary keys for simplicity. Do keep in mind that both techniques require the list elements to be hashable (e.g., integers, floats, and strings).
You can automatically get all the unique elements of your list merely by converting it to a set and then back to a list. A Python set is a separate data structure that only includes unique items.
This code snippet uses a set to remove duplicates from the list of numbers:
There’s one big caveat for this method: set() does not necessarily retain the original ordering of your list. If you do need the list elements to remain in their current order, try dict.fromkeys() instead:
You may choose to convert your list into a dictionary and then back to a list again if you need to retain the order of your list elements. This option first builds a dictionary with the items in your list as its keys. It then transfers the keys back to a list while preserving the first occurrence of each value.
Performance and Complexity
When considering performance and complexity of various Python list techniques, you’ll want to consider how the method scales as the list grows. One common way to describe complexity, big O notation, tells you about complexity in an abstract sense. For example, O(1) means the technique’s runtime does not grow with the size of the list; O(n) means the method’s calculations scale linearly with the list size; O(n2) approaches scale quadratically as the list grows; and so on.
The list removal techniques you’ve seen in this article mostly grow like O(n), meaning their work grows roughly proportionally to the number of elements. There are two noteworthy exceptions, though. The .pop() method is O(1) when you don’t pass an index argument; that is, it’s O(1) when removing the last element since Python does not need to search for or shift any elements.
Using a while loop to remove all occurrences or to filter based on a condition, however, can scale like O(n2). Each iteration may perform a linear-time membership or condition check as well as a linear-time .remove() operation. Due to this inefficiency, this style of while loops should generally be avoided for removing many items, especially for large lists.
Method Comparison in Python: Remove Items from List
With all the different ways to remove elements from a list in Python, it’s helpful to have one condensed table to compare them. Review the following to brush up and choose the best technique for your use case:
Method | Type | Removes | In place? | Returns | Best Use Case |
|---|---|---|---|---|---|
.remove(value) | Method | First matching value | Yes | None | Known value to remove |
.pop(index) | Method | One item by index | Yes | Removed item | Known index to remove; removed item needed |
del list[start:stop] | Statement | Range of items | Yes | – | Removing list slices |
.clear() | Method | All items | Yes | – | Emptying a list |
List comprehension | Expression | Multiple matching items | No | New list | Removing all matches or filtering by a condition |
filter() | Function | Multiple matching items | No | filter iterator | Lazy filtering, especially with an existing function |
while loop + .remove() | Loop + method | All value occurrences | Yes | – | Repeatedly modifying a small existing list |
list(set(...)) | Conversion | Duplicates | No | New list | Unique values without preserving order |
list(dict.fromkeys(...)) | Conversion | Duplicates | No | New list | Unique values while preserving order |
Wrapping Up
When it comes time to trim down your Python list, you have plenty of options to remove elements. You can get rid of single elements by value with .remove() or by index with .pop(), a range of elements with del, or all list items with .clear(). Try a list comprehension or filter() if you need to toss out all occurrences based on a value or condition. And don’t forget about set() or dict.fromkeys() to help remove duplicates.
One final warning: try not to remove elements while iterating over that same list. Removing items shifts the position of later elements, so your loop could accidentally skip items. Instead, list comprehensions let you build a filtered list without modifying your original.
Think you’ve mastered removing items from a list in Python? Test yourself by interacting with the AI Tutor, starting with this prompt:
- Fix "Invalid Syntax" in Python (8 Common Causes)
- The or Operator in Python: Complete Guide with Examples
- Python reduce(): The Complete Guide (With Examples)
- Master Python Filter: Syntax, Examples, and Best Practices
- Python Max Int: Understanding Arbitrary Precision Integers
- Python KeyError Exceptions: Causes and Fixes Explained
- Python Null (None): Guide to Missing Values and NoneType
- Python Backend Development: Build Your First API
- Python Backend Frameworks: How to Choose the Right One
- Python Return Multiple Values: 4 Methods & Examples
Kimberly Fessel