You are currently viewing Remove Item from Python List

How to remove an element/item from the python list? Using the remove() method you can remove a particular element from the list by passing the element. In this article, we will discuss several ways to remove elements from a List in python. A list is an ordered data structure that is used to store multiple data type elements.

Advertisements

Following are the methods we used in this article to remove an element from Python List.

  1. list.remove() to remove the element from the list.
  2. The discard() method available in Set can also be used. To do this we need to convert the list to a set.
  3. Using the del keyword is another approach to remove an element by index.
  4. We can also remove particular elements through conditions using filter() with a lambda expression and List comprehension.
  5. Finally, the pop() method is also used in this article to remove the last item by default or any item by passing the index position.

1. Quick Examples of Removing Elements from the List

Following are quick examples of how to remove an element from a python list.


# Quick Examples

# Example 1 - Consider the list of strings
countries=['India','USA','Japan','Ukrain','China','Mexico']

# Example 2 - Using remove()
countries.remove('China')

# Example 3 - Remove() with index
countries.remove(countries[3])

# Example 4 - Using del
del countries[3]

# Example 5 - Using filter()
result = list(filter(lambda element: element != 'India',countries))

# Example 6 - filter() with index
result = list(filter(lambda element: element != countries[0],countries))

# Example 7 - Using List Comprehension
result = [element for element in countries if element != 'USA']

# Example 8 - List Comprehension with index
result = [element for element in countries if element != countries[1]]

# Example 9 - Using pop()
removed = countries.pop(0)

# Example 10 - Using discard()
countries=set(countries)
countries.discard('to')

2. Python List remove() Method

The Python list.remove() method is used to remove the specified element/item from the given list of elements. By using this you can specify the element/item you wanted to remove as a parameter or use the position to get the element and use it.

When you try to remove an element that does not exist in the list, it will return a ValueError hence it is always a good practice to remove an element either by checking if the element exists or using a try-except block.

2.1 remove() Syntax

Look at the syntax below on how to delete an item from the list by passing the element or the index position to the remove() method.


# Syntax

# Using remove() with element
mylist.remove(element)

# Using remove() with index position
mylist.remove(mylist[index])

Here, mylist is the list object from where the element is removed.

2.2 Remove Element by Value

Let’s create a list of strings that holds 6 countries and remove the element China from it.


# Consider the list of strings
countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using remove()
countries.remove('China')
print("Final List: ",countries)

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Final List:  ['India', 'USA', 'Japan', 'Ukrain', 'Mexico']

Here, we specified an element inside the remove() method and it got removed from the list. The final list has 5 elements.

2.3 Remove Element by Index

Let’s use the same countries list and remove the same China element from it by using an index. You can use this only if you know the index of the element you wanted to remove. Note that this method doesn’t take the index as an argument however, you can get the element by index using list[index] and use the value to the method.


# Consider the list of strings
countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using remove() by Index
countries.remove(countries[4])
print("Final List: ",countries)

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Final List:  ['India', 'USA', 'Japan', 'Ukrain', 'Mexico']

Here, we got the value of index 4 which is China, and passed it to the remove() method, so the element present at index 4 got removed. Now the final list has 5 elements.

2.4 Remove Element that Not Exist

Let’s remove the element that doesn’t exists in the list. As I said above, it returns a ValueError. In this case, we need to handle the error using a try-except block.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using remove()
try:
  countries.remove('Colombia')
  print("removed")
except:
  print("Element not exists!")

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Element not exists!

3. Remove Element from List using discard()

In Python, the set.discard() method is used to delete the specified element from the given set of elements. To use this, first, you need to convert our List to Set. After removing the element from the set, convert it back to a list.

3.1 discard() Syntax

Look at the syntax below on how to convert the list to a set and apply the discard() method on it.


# Here, mylist1 is the input list.

# Convert List to Set
mylist=set(mylist)

# Using discard()
mylist.discard(element)

3.2 Example

Let’s create a list that holds 6 strings and remove the Japan from it by converting the list to a set.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Convert List to Set
countries=set(countries)

# Using discard()
countries.discard('Japan')
print("Final List: ",list(countries))

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Final List:  ['Ukrain', 'China', 'Mexico', 'USA', 'India']

Here, Element Japan has been removed from the set, then we converted the set back to a list.

4. Delete Element using del

The del is a keyword available in python that can be used to remove a particular element or item from the python list. Here, we need to pass the index position such that element at this position is removed.

4.1 del Syntax

Look at the syntax below.


# Syntax

# Here, mylist1 is the input list.
del mylist1[index]

4.2 Example

By using this del keyword, let’s remove an element from the list.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using del
del countries[0]
print("Final List: ",countries)

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Final List:  ['USA', 'Japan', 'Ukrain', 'China', 'Mexico']

The element present at index 0 is India and it is removed from the list.

5. Delete item using pop()

The pop() in Python will remove the last item in the list by default and returns the element that’s been removed. It is optional to pass the index position as a parameter to remove the element by index. When we display the list after pop(), it will print the list to the console without an element that was removed.

5.1 List pop() Syntax

Look at the syntax below.


# Syntax

# Here, mylist1 is the input list.
mylist1.pop(index)

5.2 Example

By using this pop() method, let’s remove an element from the list and display the element that’s been removed.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using pop()
removed=countries.pop()
print("Removed element: ",removed)
print("Final List: ",countries)

# Using pop()
removed=countries.pop(3)
print("Removed element: ",removed)
print("Final List: ",countries)

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Removed element:  Mexico
# Final List:  ['India', 'USA', 'Japan', 'Ukrain', 'China']
# Removed element:  Ukrain
# Final List:  ['India', 'USA', 'Japan', 'China']

First, we have removed the last item using pop(). and in the next example, we passed index 3 and it is removed from the list.

6. Delete item using filter()

filter() is used to return particular elements from the list. We can pass the condition such that a particular element can be removed from the list. Similarly, we can also do the same by passing the index position. Finally, we will use list() method to return the elements in a list.

6.1 filter() Syntax

Below is the syntax for filter().


# Here, mylist1 is the input list.

# Using filter()
list(filter(lambda element: element != element,mylist1))

# Using filter() with index
list(filter(lambda element: element != mylist1[index],mylist1))

6.2 Example

In the below example, first, we have removed Mexico from the countries list by specifying the element, next we passed index position 0 to remove the first element which is India.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using filter()
print("Final List: ",list(filter(lambda element: element != 'Mexico',countries)))

# Using filter() with index
print("Final List: ",list(filter(lambda element: element != countries[0],countries)))

7. Delete item using List comprehension

We can pass the condition inside the list comprehension such that element can be deleted from the list. Here, first, we need to iterate all items in the list by ignoring the element to be removed. We will return all the elements except the element by checking it using not equal operator.

7.1 Syntax

Look at the syntax that uses List comprehension.


# Here, mylist1 is the input list.

# Using List comprehension
[item for item in mylist1 if item != element]

# Using List comprehension with index
[item for item in mylist1 if item != mylist1[index]]

7.2 Example

Let’s create a list and remove elements using List comprehension. In the below example, first, we have removed Mexico from the countries list by specifying the element, next we passed index position 0 to remove the first element which is India.


countries=['India','USA','Japan','Ukrain','China','Mexico']
print("Actual List: ",countries)

# Using filter()
print("Final List: ",[element for element in countries if element != 'Mexico'])

# Using filter() with index
print("Final List: ",[element for element in countries if element != countries[0]])

# Output:
# Actual List:  ['India', 'USA', 'Japan', 'Ukrain', 'China', 'Mexico']
# Final List:  ['India', 'USA', 'Japan', 'Ukrain', 'China']
# Final List:  ['USA', 'Japan', 'Ukrain', 'China', 'Mexico']

8. Conclusion

In this article, you have learned different ways to remove an element from the list in Python. The remove() method is used to remove the element by value, when the value is not present it returns a ValueError. when using discard(), make sure that you need to convert the list to set. Also, learned to delete elements using pop(), filter(), and List comprehension.

References