How to Remove an Element from the List in Python[7 ways]

Do you want to remove a list item in Python? In this tutorial, I will explain “How to Remove an Element from the List in Python” with practical examples.

Suppose there is a list of data like this: “data = [2,4,6,7,8,10….]”; I need to remove 7 from the data and want this as an output: “[2,4,6,8,10]”. I will show you how you can do this in Python.

While working on e-commerce website projects like Flipkart, Amazon, or Myntra, I was required to create functionality to remove items from the cart. During my research, I found 7 different ways and techniques to remove an element from the list in Python.

How to Remove an Element from the List in Python using the remove() method

We will use a built-in method called remove() in Python to remove an element from a list in Python. You have to give the element name as a parameter in the remove() method so it will find the first occurrence of the given input and remove that value from the original list.

Syntax

  • list.remove(): This method is useful for list collection only and requires 1 parameter as any values like string, number, etc.
  • remove() method will modify the original list in Python
  • If the given value is not there inside the list, then it will show an error like this
    ValueError: list.remove(x): x not in list

Let’s take one practical example of How to remove an element from the list in Python using the remove() method.

list_of_cities = ["New York", "Los Angeles","Beijing", "Houston", "Dallas", "Beijing"]

# Removing an element from original list
list_of_cities.remove("Beijing")
print(list_of_cities)
How to Remove an Element from the List in Python using remove() method

In the above code, you can see the list of cities that included “Beijing” city two times on index positions 2 and 5, and then using the remove() method to delete the element named “Beijing” like this “list_of_cities.remove(“Beijing”)“.

It will remove only the first occurrence of the given value, which is why it eliminates only the value of the second index.

How to Remove an Item from a List in Python Using the pop() Method

Generally, the pop() method removes the last element of the collection, but in Python, you can also give the index position as a parameter of the list.pop() method to remove an element from the list in Python.

READ:  How to find the size of a Python list

Syntax

list.pop(index_position)
  • list.pop(index_position): This method will work for list collection only, and this
  • It will remove the item from the original list in Python based on the given index value.
  • If you do not provide any index value, it will remove the last element from the list by default.

Let’s understand this method with a practical example of removing the items from a list python by index.

list_of_cars = ['tesla', 'toyota', 'bmw', 'ford']

user_input = input("Enter the car name you want to remove : ").lower()
index_value = list_of_cars.index(user_input)
print("Removed : ",list_of_cars.pop(index_value))
print("Updated List :",list_of_cars)
How to remove an item from a list in Python using the pop() method

In this code, we created one list of cars and took user input to ask which car they want to remove “user_input = input(“Enter the car name you want to remove: “).lower()“, you can see we are using the lower() method to convert all the characters in lowercase of user input.

Then, we created a variable called index_value to get the index position of the user_input value using the list.index() method “index_value = list_of_cars.index(user_input)“.

After getting the user_input’s index position, we removed that element by its index value using a pop() method like this: “list_of_cars.pop(index_value)”

How to Delete An Element From a List Using the del Keyword in Python

The del keyword is used to delete elements, objects, and variables in Python. You can remove a single element or multiple by using the del keyword. It takes a list-slicing expression to remove an element from the list in Python.

Syntax to remove a single element from the list

del list[index_position]
  • del list[index_position] : You can give an element’s index position, and it will delete that element from the original list in Python

Syntax to remove multiple elements from the list

del list[start_index : end_index + 1]

Here, you can give a start index and end index to remove multiple elements from the list in Python, and there is one more option as updation in this list slicing, like
del list[start_index : end_index + 1 : steps].

Let’s understand this with practical examples of removing single or multiple elements from the list.

customers_list = ["Alice", "Arthur", "Bob", "Carol", "Dave", "Frank", "Grace", "Harry"]

del customers_list[3]
print(customers_list)
How to delete an element from a list using the del keyword in Python

In the above code, we have one list of customer names, and we are trying to remove the element “del customers_list[3]” at the 3rd index position.

Let’s try to remove multiple elements from the list in Python:

customers_list = ["Alice", "Arthur", "Bob", "Carol", "Dave", "Frank", "Grace", "Harry"]

del customers_list[0:4]
print(customers_list)
python remove multiple item from a list

In this code, we’ve given a start index and end index as 0 : 4, and it will remove the elements from the 0 index position to the 3rd index position “del customers_list[0:4]“.

Remove an Element From the List in Python Using List Comprehension

When we need to remove all occurrences from the list in Python, we can use List Comprehension. In one line of code, you can give conditions inside a for loop and create a filtered copy of the original list.

READ:  Constructor in Python [Complete Guide]

Also, when we use List Comprehension, it will not affect the original list.

Syntax

filtered_list = [index for index in list if condition]
  • First, it will start from the for loop “for index in list” in this line, it will initialize a variable and iterate over every element inside the list, and then it will check for the condition,
    if condition“.
list_of_states = ["Alabama","Fujian","Alaska", "Arizona", "Fujian","Connecticut", "Florida","Fujian", "Georgia","Fujian"]

filtered_list = [state for state in list_of_states if state != "Fujian"]
print(filtered_list)
Remove an element from the list in Python using List comprehension

In the above code, We have one list of states where the state “Fujian” is repeated 4 times and I want to remove all of them from the list, that’s why we are using list comprehension to remove all the occurrences of the “Fujian” state.

We’ve created a variable named “filtered_list”, and initialized a variable inside for loop named state, which will iterate over all the elements inside list_of_states like this
“for state in list_of_states”.

Then we’ve put the condition if state != “Fujian“. When the state iterates to “Fujian”, it will not include that element inside filtered_list according to our condition.

How to Remove an Item From the List in Python Using the Filter() Method

We can also use the filter() method with the lambda function to remove an element from the list in Python. We will use the same logic and conditions as in our previous example.

Syntax

filter(function, collection) 
  • Filter (function, collection): This method will apply conditions to each element of the original list.
  • Also, we will use the lambda function as 1st argument to create a condition and include list_name in the 2nd argument of filter().

Let’s understand it with a practical example of removing an element from the list in Python using the filter() method.

employees_data = ["Oliver", "Harry", "George","William","Harry"]

print("Original List :", employees_data)
filtered_list = filter(lambda employee: employee!='Harry',employees_data)
print("Filtered List: ", list(filtered_list))
How to remove an item from the list in Python using the filter() method

In the above code, we have a list named employee_data, and we needed to remove “Harry” from the list, so we’ve used the filter() method. It accepts 2 parameters as function and collection “filter(lambda employee: employee!=’Harry’,employees_data)“,

Also, we’ve created a lambda function as a parameter inside the filter() method. We’ve given a condition, like if the element is not equal to ‘Harry’, then added that element inside filtered_list, like this lambda employee: employee!=’Harry’.

Remove Items from the List Using List-slicing in Python

We can also remove an element by using list-slicing in Python. This is the process of modifying the list or extracting the elements based on their index positions.

We will create a new list to filter the elements based on our requirements. However, it can remove a single element at a time, and if duplicate elements are present, it will remove the first occurrence of a given value from the list.

READ:  Python Program for Selection Sort [Detail explanation with 3 Examples]

Syntax

list[start_index : end_index]

According to our requirement, the start_index should always be 0, and the end_index will be decided based on the user_input element’s index position.

Let’s see how we can remove an element from the list in Python using list slicing.

Employees_age = [23,34,65,32,42,46,34]

user_input = int(input("Enter number to remove :  "))
index_value = Employees_age.index(user_input)
filtered_list = Employees_age[0:index_value] + Employees_age[index_value+1 : ]
print(filtered_list)
Remove items from the list using list-slicing in Python

In the above code, we have a list of employee’s ages, so based on the user input number, we are finding the index position of that element by using the index() method like this
index_value = Employees_age.index(user_input)“.

Then, we created a new list named filtered_list so the modifications would not affect the original list. In filter_list, we perform list slicing and concatenation.

We gave the start index as 0, and it finds the end_index as 1 (index_value of 34) “Employees_age[0:index_value]“, and we are concatenating with another list slicing “Employees_age[index_value+1 : ]” where we gave index value + 1 as the start point which means 1 + 1, so it will start from 2 to last index of the list by default.

How to Remove an Element From the List in Python Using the Discard Method

Generally, we use the discard() method to remove elements from the set collection, but we will use this method to remove an element from the list in Python. We will perform type conversion using set(), then remove the elements, and again, we will convert the set to the list datatype using list().

Syntax

set.discard(value)
  • set.discard(value): It is designed for set collection only to remove an element and requires 1 argument.
  • If you give the value that does not exist inside the set, it will return the same set as it is.
list_of_cities = ["New York", "Los Angeles","Beijing", "Houston", "Dallas", "Beijing"]

print("Original List is :", list_of_cities)
list_of_cities = set(list_of_cities)
list_of_cities.discard("Beijing")
list_of_cities= list(list_of_cities)
print("After removing element :", list_of_cities)
How to remove an element from the list in Python using the discard method

Note: The sequence of the list will be changed if you convert it into the set collection.

In the above code, we have one list of states, and we want to remove “Beijing” from the list. So we’ve converted our original list to the set data type using set() “set(list_of_cities)” and used the discard() method “list_of_cities.discard(“Beijing”)“, which will remove the given value from the list.

Then again we are converting it to the list using a list constructor like this
list_of_cities= list(list_of_cities)“.

Conclusion

In this Python article, we’ve explored 7 different methods and techniques to remove an element from the list in Python, which are remove(), pop(), discard(), and filter(), and also some other techniques like using del keyword in Python, list comprehension and list slicing.

We’ve explained all these methods in detail and given examples with code explanations for every technique.

You may like to read: