Ways to Remove Element from List Python
In this tutorial, we will explore different ways to remove an element from a list in Python. Removing an element from a list is a very common operation in Python programming language.
A list in Python is mutable, meaning that we can easily modified, added, or removed elements from an existing list after we create it.
In other words, Python does not create a new list object while modifying it. We will understand the following topics in this tutorial:
- Removing an element by index
- Removing the first occurrence of an element by value
- Removing an element using list comprehension
- Removing elements using filter() function
- Removing elements using del statement
- Removing duplicate elements from a list
How to Remove an Element by Index
A simple way to remove an element from a list in Python is by using its index. The index of an element in a list represents its position in the list, starting from zero, one, two, and so on. We can use the built-in pop() method of the list to remove an element by its specified index.
Example 1:
# Creating a list having five elements.
list = [2, 4, 7, 8, 10]
# Removes an element at index 2.
list.pop(2)
print("Modified list:",list)
Output:
Modified list: [2, 4, 8, 10]
In this example, we have created a list of five numbers. Then, we have removed an element at index 2 using the pop() method. The pop() method returns the removed element, which we can store in a variable if needed.
Remove First Occurrence of Element by Value
Another easy way to remove an element from a list in Python is by using its value. We can use the built-in remove() function of the list to remove the first occurrence of an element with a specific value. Let’s take an example program based on it.
Example 2:
# Creating a list having four fruit elements.
fruits = ['Apple', 'Banana', 'Orange', 'Apple']
# This statement removes the first occurrence of element 'Apple'.
fruits.remove('Apple')
print(fruits)Output:
['Banana', 'Orange', 'Apple']
In this example, we have created a list of four fruits and removed the first occurrence of the element ‘Apple’ using the remove() method.
Removing Elements using List Comprehension
List comprehension is a compact way to construct a new list from an existing list based on some condition. We can use the list comprehension to remove elements from a list based on some condition.
For example, let’s write a program in Python in which we will remove all even numbers from a numeric list using list comprehension. Look at the below code to understand better.
Example 3:
# Creating a numeric list having 8 elements.
nums = [1, 2, 3, 4, 5, 7, 8]
# This statement removes all even numbers from the list.
nums = [n for n in nums if n % 2 != 0]
print(nums)
Output:
[1, 3, 5, 7]
In this example, we have created a numeric list of eight numbers. Then, we have used the concept of list comprehension to delete all even numbers from the original list. After removing all even numbers, a new list contains only the odd numbers.
Remove Elements using filter() Function
Similar to list comprehension, we can also use the built-in filter() function of Python to remove elements from a list based on some condition.
The filter() function returns an iterator object that contains only elements that fulfil the specified condition. We can convert the iterator to a list object using the list() function provided by Python list. Let’s take an example of it.
Example 4:
# Creating a numeric list having five items.
numbers = [12, 15, 17, 18, 20]
# This statement removes all even numbers from the below list.
numbers = list(filter(lambda x: x % 2 != 0, numbers))
print(numbers)
Output:
[15, 17]
In this example, we have used the filter() function that will create a new list that contains only the odd numbers from the original list. The lambda function passed to filter() specifies the test condition that items should satisfy. We have used list() function to create a list of filtered elements.
Removing Elements using Del Statement
The del statement is a simple way to remove or delete an element or a slice of elements from a list in Python. We use the del statement followed by the index or the range of index position to remove the element(s).
Note that the del statement removes the list item but does not return the deleted item. Look at the following example code.
Example 5:
# Creating a list having five cities.
city_list = ["Dhanbad", "New York", "Mumbai", "Sydney", "London"]
# This statement removes the element at index position 2.
del city_list[2]
print(city_list)
# This statement removes the elements from a range of index position 1 to 3 (excluding 3).
del city_list[1:3]
print(city_list)
Output:
['Dhanbad', 'New York', 'Sydney', 'London']
['Dhanbad', 'London']
In this example, we have used the del statement to remove or delete an element at a specific index and a range of elements based on their indices.
How to Remove Duplicate Elements from Python List?
Sometimes we may have a list containing duplicate items. It may be essential to remove them to get a unique list. In this case, we can use the set() function to remove duplicate items from a list.
However, the set() function returns a set, which is an unordered collection of unique elements. To get a sorted list of unique elements, we can convert the set to a list using the sort() function. Look at the below example code.
Example 6:
# Creating a numeric list having duplicate elements.
numbers = [10, 20, 30, 20, 40, 10, 50]
# This statement removes duplicate items.
numbers = list(set(numbers))
# This statement sorts the list elements.
numbers.sort()
print(numbers)
Output:
[10, 20, 30, 40, 50]
In this example, we have used the set() function to remove duplicates from the list of numbers. Then, we have converted the set to a list using list() function and sorted it. We have used sort() function to sort elements of the list and displayed them on the console.
Google FAQs
Q. What will happen if we will try to remove an element that is not in the list?
A: If we try to remove an element that is not in the list using the remove() method, we will get a ValueError exception. If we try to remove an element from a list using the pop() function with an invalid index, we will get an IndexError exception.
Q. Is it possible to remove multiple elements from a list using the remove() method?
A: No, the remove() method removes only the first occurrence of an element with a specific value. If you want to remove all occurrences of an element, use list comprehension or the filter() function.
In this tutorial, we have discussed different ways to remove elements from a list in Python. We have covered almost all the ways to remove an element(s) from a list. Each way has its own advantages and disadvantages, so the choice depends on the specific requirement. Hope that you will have understood all the basic points of removing an element from a list in Python.
Thanks for reading!!!




