Python Program to Remove an Item from a List

In this article, we will show how to write a Python program to remove an item or element from a List with examples. This programming language has many built-in functions to remove a list item, including using remove(), pop(), clear()functions, and the del statement. Apart from that, we can use loops or list comprehension to remove items based on the condition.

Python Program to Delete an Item from a List using remove() function

If you know the value that you want to delete, use the remove() function. The remove() method will search for the first occurrence of the given value and delete it from the list. In the program below, the first statement will remove 25 from the list, and the next one will delete 10.

a = [5, 10, 15, 20, 25, 30, 35, 40]

a.remove(25)
print(a)

a.remove(10)
print(a)
[5, 10, 15, 20, 30, 35, 40]
[5, 15, 20, 30, 35, 40]

pop()

The pop() function uses the index value as the parameter. So, if you know the index position of the item that you want to remove, use the pop() method. In the Python program to remove an item from a list below, we have shown multiple examples along with printing the popped-out value. 

  • a = n.pop(0) removes item at the first index position and stores in variable a.
  • b = n.pop(5) removes the item at the fifth index position and stores it in variable b.
  • c = n.pop(2) deletes. item at the second index position and stored in variable c.
n = [5, 10, 15, 20, 25, 30, 35, 40]

a = n.pop(0)
print(a)
print(n)

b = n.pop(5)
print(b)
print(n)

c = n.pop(2)
print(c)
print(n)
5
[10, 15, 20, 25, 30, 35, 40]
35
[10, 15, 20, 25, 30, 40]
20
[10, 15, 25, 30, 40]

Apart from the positive value, you can also try using the negative index values as the pop method arguments. Here, -1 removes the last item from the list. If you use this method without any argument, by default, it removes the last list element.

n = [5, 10, 15, 20, 25, 30, 35, 40]

a = n.pop(-1)
print(a)
print(n)

b = n.pop(-3)
print(b)
print(n)

c = n.pop()
print(c)
print(n)
40
[5, 10, 15, 20, 25, 30, 35]
25
[5, 10, 15, 20, 30, 35]
35
[5, 10, 15, 20, 30]

Python Program to Remove an Item from a List using del statement

Similar to the pop() function, the delete statement also uses the index value to remove the list items. However, del statement allows you to place either a single index or range of indexes like slicing.

In the program below, the first statement removes the list item at the third index position. The next one deletes an item from the second index position to the sixth position.  Similarly, the last statement will remove all the items from the list because we haven’t mentioned the tsar and end, so it considers 0 as the start and len- 1 as the last.

a = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]

del a[3]
print(a)

del a[2:6]
print(a)

del a[:]
print(a)
[5, 10, 15, 25, 30, 35, 40, 45, 50]
[5, 10, 40, 45, 50]
[]
Python Program to Remove an item from a List

clear()

If your goal is to remove all the existing items from a given list and keep the object, use the clear() method. The below Python program uses the clear() function to remove all the items from the list a.

a = [1, 2, 3, 4, 5, 6, 7]
print(a)

a.clear()
print(a)
[1, 2, 3, 4, 5, 6, 7]
[]

Python Program to Remove a Particular Item from a List

The above-mentioned approaches are best to remove an item based on the index position or value. However, you can use either the above options or the below-mentioned approaches to remove a particular element from the list based on the given condition.

The if statement in the below program checks whether the value five exists in list a. If true, the pop() function will remove the first occurring 5 from the list.

a = [5, 10, 15, 5, 20, 25, 5, 30]
val = 5

if val in a:
a.pop(a.index(val))

print(a)
[10, 15, 5, 20, 25, 5, 30]

If you combine the pop() method with the for loop, you can remove all the occurrences. For instance, the below program uses a for loop enumerator to iterate the list and capture the list value and the index position. The If statement checks whether the value equals five; if true, pop the index position.

a = [5, 10, 15, 5, 20, 25, 5, 30]

for i, n in enumerate(a):
if n == 5:
a.pop(i)

print(a)
[10, 15, 20, 25, 30]

This Python program uses the for loop to iterate and remove() function to delete all occurrences of the integer five item in a given list.

a = [5, 10, 15, 5, 20, 25, 5, 30]
val = 5

for i in a:
if i == 5:
a.remove(i)

print(a)
[10, 15, 20, 25, 30]

Python Program to Remove an Item from a List using List Comprehension

It is the most popular and best approach to remove any particular list element based on the condition. The below program will add all the list items to an except if the number is 5.

a = [5, 10, 15, 5, 20, 25, 5, 30]

a = [n for n in a if n != 5]
print(a)
[10, 15, 20, 25, 30]

Similarly, you can try the below code to remove the odd and even numbers from the given integer list.

[5, 10, 15, 5, 20, 25, 5, 30]
[10, 20, 30]
[5, 15, 5, 25, 5]

In this program, we used two approaches to remove duplicate list items from the given one. In both approaches, we used the count() function to find out the total number of occurrences of each element in a list. Here, the list comprehension will print the list with a count exactly equal to one. It means removing all duplicates. However, the for loop will spare at least one value.

a = [5, 10, 15, 5, 20, 25, 5, 30]
print(a)

new = [n for n in a if a.count(n) == 1]
print(new)

for n in a[:]:
while a.count(n) > 1:
a.remove(n)

print(a)
[5, 10, 15, 5, 20, 25, 5, 30]
[10, 15, 20, 25, 30]
[10, 15, 20, 25, 5, 30]

This Python program uses the filter() and lambda statement to remove the list items based on the condition where the value equals 5.

a = [5, 10, 15, 5, 20, 25, 5, 30]

a = list(filter(lambda n : n != 5, a))
print(a)
[10, 15, 20, 25, 30]