Python Remove Multiple Items From List


In this article, you will learn how Python remove multiple items from list in 6 different ways with examples. You will also learn to remove various items by their item value or index values.

A list is a collection of data of different types which is ordered and changeable.

Elements can be added, removed, or modified in a list.

To remove an element from a list we can use:

  1. remove() - remove() method removes the first occurrence of the specified value.
  2. pop() - pop() method removes the element from any given index. If index not given then removes the last element.
  3. del - del keyword removes the specified element.
  4. clear() - clear() method empties the list.

We are going to use the remove() method in this tutorial. The remove() method removes an element from a list using the specified value.

Python remove multiple items from list

    Table Of Contents

  1. Remove multiple items using loops
  2. using list comprehension
  3. using set function
  4. using filter function
  5. using hash table
  6. python remove multiple items from list by index
  7. python remove multiple items from list by value

1. Removing multiple items using loops

Loops are very useful for doing any repetitive task and here using simple logic with a loop we are going to remove multiple items from a list.

Let our list of numbers be:

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

and numbers to be removed are:

num_to_remove = [1, 3, 5, 7, 9]

To remove the numbers listed above from the list create a nested loop. The external loop iterate over the list (list of numbers to remove) taking one number at a time and the inner loop iterates over the list (list of numbers) and remove the number from the list using the remove() method.

Example 1:

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]

# looping list of numbers to remove
for n in num_to_remove:
    # taking 1 number found in the list
    # remove it from the list
    while n in num:
        num.remove(n)

print(num)

Output:

[2, 4, 6, 8, 10]

In the above code, the for loop is iterating over the list of numbers to remove. Taking each element from that list it finds the number in the list using the python while loop and removes it and keeps iterating until the number is not found in the list.

Here is 1 more way to remove elements from a list using the loops and remove() method.

Example 2:

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]
# looping list of number to remove
while num_to_remove:
    # taking last element of list
    # and also poping it from list
    i = num_to_remove.pop()
    # find and remove the element from list
    while i in num:
        num.remove(i)
print(num)

Output:

[2, 4, 6, 8, 10]

2. using list comprehension

List comprehension is a concept in Python which is used to create a new list from an existing list.

It is a single line of code in which you can add your own logic to create a new list.

Look at the code first then understand the logic.

# using list comprehension
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]

# create new list using list comprehension
num = [i for i in num if i not in num_to_remove]

print(num)

Output:

[2, 4, 6, 8, 10]

The above code uses list comprehension to create a new list from an existing list. It takes each element (i) from the existing list and checks if it is not in the list of numbers to remove. If it is not in the list of numbers then include it in the new list.


3. using the set function

The set() function removes the duplicate elements passed to it and returns a new set having all the unique elements.

It also has a difference() method (or - operator) which returns a new set with all the elements that are present in the first set but not in the second set.

We can also remove multiple elements from a list using this. Let's see how.

# using set function
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]

# removing the elements from the list
# using set() and - operator
num = list(set(num) - set(num_to_remove))

# or
# num = list(set(num).difference(set(num_to_remove)))

print(num)

Output:

[2, 4, 6, 8, 10]

Note: The issue with the above method is that it will also remove the duplicate values of the numbers to remove from the list which is not required. So use only when you have a unique set of elements.


4. using the filter function

The filter() function is used to filter out the elements from a list based on a condition. It accepts a function as an argument and returns a new list with all the elements that satisfy the condition.

Look at the code first then understand the logic.

# using filter function
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]

# create new list using filter function
num = list(filter(lambda x: x not in num_to_remove, num))
print(num)

Output:

[2, 4, 6, 8, 10]

The above code is using filter() function to filter out the elements from a list based on a condition. It takes each element (x) from the list of numbers to remove and checks if it is not in the list of numbers. If it is not in the list of numbers then include it in the new list.


5. using hash table

The hash table is a data structure that stores key-value pairs or data. It is also called a dictionary in Python.

The idea here is to convert the list of numbers to a dictionary where the key is the number and the value is 1 (for simplicity). Then we can use the for loop to set the value to 0 for all the elements in the list of numbers to remove.

Finally, use list comprehension to get all keys whose value is greater than 0.

# using hash table
num = [5, 8, 3, 7, 12, 10, 0, 2]
num_to_remove = [2, 12, 0, 3]

# create a hash table
# where key is list element and value 1
num = {i: 1 for i in num}

# using for loop set value to 0
# for all elements in num_to_remove
for i in num_to_remove:
    num[i] = 0

# using list comprehension to get all keys
# whose value is greater than 0
num = [i for i in num if num[i] > 0]
print(num)

Output:

[2, 4, 6, 8, 10]

The above code is using a hash table to remove the duplicate elements from a list.

Note: Just like set() function it also removes the duplicate values of the elements from list which is not required.


Python remove multiple items from list by index

To remove an element by its index value we can use the del keyword.

It supports positive, negative, and a range of indexes (for slicing).

For example:

# using del keyword
num = [1, 2, 3, 4, 5, 6]

# remove element by index
del num[0]
print(num) # [2, 3, 4, 5, 6]

del num[-1]
print(num) # [2, 3, 4, 5]

del num[1:3]
print(num) # [2, 5]

Output:

[2, 3, 4, 5, 6]
[2, 3, 4, 5]
[2, 5]

Now to remove multiple elements from a list we can use the del keyword with a range of indexes with loop.

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num_to_remove = [1, 3, 5, 7, 9]

# use del to remove items from a list
for i in num_to_remove:
    del num[num.index(i)]

print(num)

Output:

[2, 4, 6, 8, 10]

The above code is using del keyword to remove multiple elements from a list.


Python remove multiple items from list by value

To remove an element by its value we can use the remove() method.

Above all of the methods, we have discussed is using the remove() method.

  1. Using loop
  2. Using list comprehension
  3. Using hash table
  4. Using set function
  5. Using filter function

Conclusion

In this article, we have discussed multiple different ways of how Python remove multiple items from list.

Using list comprehension is a cleaner way to remove multiple items from a list.