Remove Function in Python

Python is a high-level programming language that is widely used for various purposes such as web development, scientific computing, data analysis, artificial intelligence, and more. One of the key features of Python is its rich set of built-in functions and modules. The ‘remove’ function is one such built-in function in Python that is used to remove an item from a list. In this article, we will discuss the remove function in Python in detail, including its syntax, return value, and examples.
What is the Remove() Function in Python?
The ‘remove’ function is a built-in function in Python that is used to remove a specified item from a list. A list is an ordered collection of elements that can be of any data type such as strings, integers, or even other lists. The ‘remove’ function is used to delete a specific element from a list, thereby modifying the list. This function is particularly useful when you need to remove an item from a list based on a certain condition.
Note that the ‘remove’ function only removes the first occurrence of the specified item from the list. If the item appears multiple times in the list, only the first occurrence will be removed. If you want to remove all occurrences of the item, you can use a loop to iterate over the list and remove the item from each occurrence.
Syntax of Remove Function in Python
The syntax of the remove function in Python is as follows:
Here, ‘list_name’ refers to the name of the list from which you want to remove the item, and ‘item’ refers to the item that you want to remove from the list. Note that the ‘remove’ function is a list method, which means it is called using the dot notation on a list object.
Return Value of Remove Function in Python
The remove function in Python does not return anything. It modifies the original list by removing the specified item. If the specified item is not found in the list, the ‘remove’ function raises a ValueError.
Time Complexity of Remove Function in Python
In Python, the time complexity of the ‘remove’ function is O(n), where n is the length of the list. This is because the function needs to search the list sequentially to find the first occurrence of the specified item and then remove it. If the item is at the beginning of the list, the function will have to search through the entire list, resulting in a worst-case time complexity of O(n).
It is important to note that if you are removing multiple occurrences of the same item from the list, the time complexity can be significantly higher as you may need to iterate over the list multiple times. In such cases, it may be more efficient to use other data structures or algorithms that have better time complexity for the specific task you are trying to accomplish.
Examples of Remove Function in Python
Now, let’s look at some examples of how to use the remove function in Python.
Example 1: Remove Function in Python
Removing an Item from a List
Code Implementation:
Output:
Explanation
In this example, we have a list of fruits. We want to remove the ‘banana’ from the list using the ‘remove’ function. The ‘remove’ function removes the ‘banana’ from the list, and the modified list is printed.
Example 2: Remove Function in Python
Removing an Item Based on Condition
Code Implementation:
Output:
Explanation
In this example, we have a list of numbers. We want to remove all even numbers from the list using the ‘remove’ function. We use a for loop to iterate over the list and check if each number is even or not using the modulus operator. If a number is even, we remove it from the list using the ‘remove’ function. The modified list is printed after all even numbers have been removed.
Example 3: Remove Function in Python
Handling ValueErrors
Code Implementation:
Output:
Explanation
In this example, we have a list of fruits. We try to remove the item which is not present in the list, We use the remove function which gives the value error, In this example as we observe the mango is not present in the list, we use except statement to handle the error in which we use the print function to print "The specified item is not found in the list."
Summary
The remove function in Python is a list method that allows you to remove the first occurrence of a specified item from a list. The function takes only one parameter, which is the item that you want to remove from the list. The syntax of the ‘remove’ function is simple and easy to use. However, it is important to note that the time complexity of the ‘remove’ function is O(n), where n is the length of the list. This means that if you are removing multiple occurrences of the same item from the list, the time complexity can be significantly higher, and it may be more efficient to use other data structures or algorithms.
Overall, the ‘remove’ function is a useful tool for working with lists in Python and can make it easier to manipulate and modify data in your code.
Frequently Asked Questions(FAQs)
Here are some FAQs on the remove function in python
Q1: What happens if the specified item is not found in the list?
A: If the specified item is not found in the list, the ‘remove’ function raises a ValueError. You can use a try-except block to catch this error and handle it in your code.
Q2: Can I use the ‘remove’ function on other data structures besides lists?
Ans: No, the ‘remove’ function is a list method and can only be used on lists in Python.
Q3: What is the difference between the ‘remove’ function and the ‘del’ statement in Python?
Ans: The ‘remove’ function is a list method that removes the first occurrence of a specified item from a list, while the ‘del’ statement is a built-in Python statement that can be used to delete a specific element or slice of a list. The ‘del’ statement can also be used to delete variables or other objects in Python.
Q4: How do I remove all occurrences of a specific item from a list?
Ans: The ‘remove’ function only removes the first occurrence of a specified item from a list. To remove all occurrences of a specific item from a list, you can use a loop to iterate over the list and remove the item from each occurrence.
Q5: Can I use the ‘remove’ function to remove items from a tuple or a set?
Ans: No, the ‘remove’ function is a list method and can only be used on lists in Python. Tuples and sets are immutable data structures, which means that once they are created, their contents cannot be modified. If you want to modify the contents of a tuple or a set, you need to create a new tuple or set with the modified contents.
Q6: Can I remove multiple items from a list using the ‘remove’ function?
Ans: No, the ‘remove’ function only removes the first occurrence of a specified item from a list. If you want to remove multiple items from a list, you can use a loop to iterate over the list and remove each item individually, or you can use list comprehension to create a new list with the desired items removed.
Что такое remove в python
Output:
Python List remove() Syntax
Syntax: list_name.remove(obj)
- obj: object to be removed from the list
Note: It removes the first occurrence of the object from the list.
remove() in Python Example
Remove an element from the list
In this example, we are showing how we can use the remove() function with the Python list. Remove function removes the specified element’s first occurrence in the list.
Python List .remove() — How to Remove an Item from a List in Python

Dionysia Lemonaki

In this article, you’ll learn how to use Python’s built-in remove() list method.
By the end, you’ll know how to use remove() to remove an item from a list in Python.
Here is what we will cover:
The remove() Method — A Syntax Overview
The remove() method is one of the ways you can remove elements from a list in Python.
The remove() method removes an item from a list by its value and not by its index number.
The general syntax of the remove() method looks like this:
Let’s break it down:
- list_name is the name of the list you’re working with.
- remove() is one of Python’s built-in list methods.
- remove() takes one single required argument. If you do not provide that, you’ll get a TypeError – specifically you’ll get a TypeError: list.remove() takes exactly one argument (0 given) error.
- value is the specific value of the item that you want to remove from list_name .
The remove() method does not return the value that has been removed but instead just returns None , meaning there is no return value.
If you need to remove an item by its index number and/or for some reason you want to return (save) the value you removed, use the pop() method instead.
How to Remove an Element from a List Using the remove() Method in Python
To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method.
remove() will search the list to find it and remove it.
If you specify a value that is not contained in the list, then you’ll get an error – specifically the error will be a ValueError :
To avoid this error from happening, you could first check to see if the value you want to remove is in the list to begin with, using the in keyword.
It will return a Boolean value – True if the item is in the list or False if the value is not in the list.
Another way to avoid this error is to create a condition that essentially says, «If this value is part of the list then delete it. If it doesn’t exist, then show a message that says it is not contained in the list».
Now, instead of getting a Python error when you’re trying to delete a certain value that doesn’t exist, you get a message returned, saying the item you wanted to delete is not in the list you’re working with.
The remove() Method Removes the First Occurrence of an Item in a List
A thing to keep in mind when using the remove() method is that it will search for and will remove only the first instance of an item.
This means that if in the list there is more than one instance of the item whose value you have passed as an argument to the method, then only the first occurrence will be removed.
Let’s look at the following example:
In the example above, the item with the value of Python appeared three times in the list.
When remove() was used, only the first matching instance was removed – the one following the JavaScript value and preceeding the Java value.
The other two occurrences of Python remain in the list.
What happens though when you want to remove all occurrences of an item?
Using remove() alone does not accomplish that, and you may not want to just remove the first instance of the item you specified.
How to Remove All Instances of an Item in A List in Python
One of the ways to remove all occurrences of an item inside a list is to use list comprehension.
List comprehension creates a new list from an existing list, or creates what is called a sublist.
This will not modify your original list, but will instead create a new one that satisfies a condition you set.
In the example above, there is the orginal programming_languages list.
Then, a new list (or sublist) is returned.
The items contained in the sublist have to meet a condition. The condition was that if an item in the original list has a value of Python , it would not be part of the sublist.
Now, if you don’t want to create a new list, but instead want to modify the already existing list in-place, then use the slice assignment combined with list comprehension.
With the slice assignment, you can modify and replace certain parts (or slices) of a list.
To replace the whole list, use the [:] slicing syntax, along with list comprehension.
The list comprehension sets the condition that any item with a value of Python will no longer be a part of the list.
Conclusion
And there you have it! You now know how to remove a list item in Python using the remove() method. You also saw some ways of removing all occurrences of an item in a list in Python.
I hope you found this article useful.
To learn more about the Python programming language, check out freeCodeCamp’s Scientific Computing with Python Certification.
You’ll start from the basics and learn in an interacitve and beginner-friendly way. You’ll also build five projects at the end to put into practice and help reinforce what you’ve learned.
Python List Remove, Pop and Clear
In python, remove(), pop(), and clear() are list functions used to remove elements from a list. These functions have different functionalities and have their own specific use cases. In this article, we’ll look at these methods, their syntax, and their differences with each other.
Before we proceed, here’s a quick refresher on python lists – Lists are used to store an ordered collection of items. These items can be any type of object from numbers to strings or even another list. This makes lists are one of the most versatile data structures in python to store a collection of objects. For more, check out our guide on lists and other data structures in python.
How to remove elements from a list in python?
Lists are quite useful when it comes to storing sequences or collections and are frequently used in python. It may happen that you require to remove elements from a list. To do so, list functions remove() , pop() , and clear() are commonly used.
The remove() function
remove() is a list function in python used to remove an element by value. It removes the first occurrence of the value from the list. The following is the syntax:
Here, sample_list is the list to remove the element from and x is the element to be removed.
The remove() function does not return any value (It returns None )
If the element is not present in the list, a ValueError gets raised on using the function.
Example 1: Removing an element from a list
In the above example, we remove the element ‘b’ from the lists ls1 and ls2 . In ls1 since ‘b’ only occurs once, it gets removed from the list altogether. But in ls2 we have the element ‘b’ at two instances, here, the first occurrence of the element ‘b’ gets removed on using the remove() function.
Example 2: Removing an element not present in the list
In the above example, we get a ValueError on removing the element ‘e’ which is not present in the list.
The pop() function
pop() is a list function used to remove a value based on its index. The following is the syntax.
Here, sample_list is the list to remove the element from and i is the index of the element to be removed. If the index is not provided, it removes the last element from the list.
The pop() function returns the element removed (or “popped out”)
Example 1: Removing an element using index
In the above example, we remove the third element from the list ls by passing the index 2 as argument to the pop() function. Also, note that unlike the remove() function, the pop function returns the element removed.
Example 2: Using pop() without index
In the above example, we do not pass an argument to the pop() function. In such a case, it removes the last element from the list.
The clear() function
clear() is a list function used to remove all the elements from a list. The following is the syntax:
Here, sample_list is the list to be cleared. It does not take any arguments.
The clear() function does not return any value. (It returns None )
Example: Remove all the items from a list
In the above example, the clear() function is used to remove all the items from the list ls .
For more on list functions refer to the python docs.
Using the del statement
Alternatively, you can use the del statement in python to remove elements from a list. Using del you can not only remove individual elements but also ranges.
Example 1: Remove an item using the del statement
In the above example, the del statement is used to remove the third element from the list ls
Example 2: Removing a range of elements from a list
In the above example, we remove a range of elements using a slice 1:3 on the list.
Tutorials on python lists:
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.