Home Python List Methods remove() Python list.remove() Method Definition remove(value) searches the list from left to right and deletes the first item that is equal (==) to value. Later duplicates stay where they are.
It returns None. If no item matches, it raises ValueError, so check with value in lst first or catch the exception when a missing value is normal.
list.remove() is an instance method of the built-in list type: call it on a list with dot notation, such as basket.remove(...). No import is needed.
Syntax Pass the value to delete, not its position.
Call What it does ["a", "b", "a"].remove("a")Returns None; the list becomes ['b', 'a']. [1, 2].remove(1.0)Returns None; the list becomes [2], because 1.0 == 1. [1, 2].remove(3)Raises ValueError: no item equals 3.
Pick the tool by what you know: the value, use remove(); the position, use pop() (returns the item) or del lst[i]; everything, use clear() .
Method signatures list.remove(value)
# returns: None; the first item equal to value is gone
# raises: ValueError when no item equals value Parameters Parameter Description valueThe value to look for. Items are compared with ==, so the match does not have to be the same object, and strings must match exactly, including case.
Return Value Return Value Description NoneAlways None. The list is one item shorter.
Python Examples 1. First match only 2. Missing and repeated 3. remove vs pop vs del vs clear Example 1 — Take Apples Out of a Grocery Basket The basket holds two apples. Each remove() call takes out only one, starting from the left.
basket = ["apple", "milk", "apple", "bread"]
basket.remove("apple")
print(basket)
print(basket.count("apple"), basket.index("apple"))
basket.remove("apple")
print(basket, "apple" in basket) ['milk', 'apple', 'bread']
1 1
['milk', 'bread'] False How It Works Leftmost wins. The first call deletes the apple at index 0 and leaves the second apple alone.One apple left. count() still finds one, and index() shows it has moved to position 1.Call again for the next. A second remove("apple") deletes the remaining apple.Membership check. "apple" in basket is now False.Example 2 — Unsubscribe Email Addresses From a Mailing List Handle an address that is not on the list, then remove every copy of a duplicated one.
How It Works Missing value raises. zed is not in the list, so remove() raises ValueError and the handler prints a friendly line.Remove every copy. The while ... in loop calls remove() until no "ann" address is left, which takes two passes here.Comprehension builds a new list. Filtering with a condition creates kept without touching subscribers; it is the usual way to drop many items at once.Original untouched. subscribers still holds two addresses after the comprehension.Example 3 — Edit a Playlist With Four Removal Tools Delete by value, by position with and without keeping the item, and then everything at once.
playlist = ["Intro", "Storm", "Tide", "Storm", "Outro"]
playlist.remove("Storm")
print(playlist)
skipped = playlist.pop(1)
print(skipped, playlist)
del playlist[0]
print(playlist)
playlist.clear()
print(playlist) How It Works By value. remove("Storm") deletes the first Storm; the second one, further right, survives.By position, keep the item. pop(1) removes "Tide" from index 1 and returns it into skipped.By position, discard it. del playlist[0] removes "Intro" and gives nothing back.Everything. clear() empties the list in place, leaving [].Frequently Asked Questions Does list.remove() delete all matching items? No, only the first one. Call it in a while value in lst loop, or build a new list with a comprehension, to remove every match.
What happens if the value is not in the list? remove() raises ValueError. Check with value in lst first, or wrap the call in try/except ValueError.
What is the difference between remove() and pop()? remove() deletes by value and returns None. pop() deletes by position and returns the removed item.
How do I remove an item by its index? Use lst.pop(i) if you want the item back, or del lst[i] if you do not. remove() always expects a value.
Why does removing items in a for loop skip some? Each removal shifts the later items one place left, so the loop moves past the item that slid into the current position. Loop over lst[:] or use a comprehension instead.
Next: list.reverse() Reverse the order of the items in place.
reverse() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion