How to Remove an Element from a List in Python

While working on a data-cleaning project for a retail company in the USA, I had to remove certain unwanted values from a Python list.

At first glance, this sounds like a simple task. But as I dug deeper, I realized there are multiple ways to remove an element from a list in Python. Each method has its own use case, and choosing the right one can save time and avoid errors.

In this tutorial, I’ll walk you through seven different methods to remove elements from a Python list. I’ll also share full code examples and explain each method in simple words.

Method 1: Use Python’s 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 inside the list, then it will show an error like this “ValueError: list.remove(x): x not in list”
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)

You can see the output in the screenshot below.

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

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

Method 2: Use Python’s 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 to the list.pop() method to remove an element from the list in Python.

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.
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)

You can see the output in the screenshot below.

How to remove an item from a list in Python using the pop() method

I 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)”.

Method 3: Use 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.

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 updating in this list slicing, like del list[start_index: end_index + 1: steps].

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

del customers_list[3]
print(customers_list)

You can see the output in the screenshot below.

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.

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

del customers_list[0:4]
print(customers_list)

You can see the output in the screenshot below.

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]”.

Method 4: Use List Comprehension in Python

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.

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)

You can see the output in the screenshot below.

Remove an element from the list in Python using List comprehension

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

Method 5: Use Python’s Filter() Method

We can also use the filter() method with a 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().
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))

You can see the output in the screenshot below.

How to remove an item from the list in Python using the filter() method

I’ve created a lambda function as a parameter inside the filter() method. I’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’.

Method 6: Use List Slicing in Python

I 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.

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.

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)

You can see the output in the screenshot below.

Remove items from the list using list-slicing in Python

In the above code, we have a list of employees’ 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)”.

Method 7: Use the Discard Method

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

Syntax

set.discard(value)
  • set.discard(value): It is designed for a set collection only to remove an element and requires 1 argument.
  • If you give a 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)

You can see the output in the screenshot below.

How to remove an element from the list in Python using the discard method

I 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.

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 the 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.