Remove multiple elements from python list

Python List

In this article, we are going to learn how to Remove multiple elements from python list with code examples. We are going to use set, list comprehension list append() methods to remove multiple elements from the Python list.

1. Using Python SET


in this example program, we are converting the list into a set and finding the differences between two sets, and returning a result list.

Steps to delete elements from Python list with Set

  • First we will convert the original list to a SET.
  • We have converted the list of duplicate items to the SET.
  • In the result list, we found the difference between these two sets and it gave us the results after removing the duplicate values.
  • Finally, display result using using print() method.
#program to remove multiple elements from python list

mylist = ['C#','lang','Go','Data','C#','16','17','35','68']

#item  to delete from list

list_item_to_remove = ['16','Go','Data']

#converting the list into a set and removing elements

result_lst = list(set(mylist) - set(list_item_to_remove))

print('list after removing elements =\n',result_lst)

Output

list after removing elements =
 ['68', '17', 'C#', '35', 'lang']

2. For loop to Remove multiple elements from Python list


  • In this example, iterating over each element of a list and applying a condition to identify the list elements that need to remove.
  • condition is if list element is divisble to 4, then we will remov that from the list using the list remove() method.
  • Once we are done removing the item, we are printing the result list.
#program to remove multiple elements from python list

my_list = [4,16,17,32,40,15,18,19]

for item in list(my_list): 

 #removing element which are divible by 4

    if item % 4 == 0: 
      my_list.remove(item)

print('list after removing elements =\n',my_list)

Output

list after removing elements =
 [17, 15, 18, 19]

3. List comprehension to Remove multiple elements by condition


Here in this example, we are using the list comprehension to delete multiple items from the list that are divisible by 4.The resulted list will contain the elements which will not be divisible by 4.

#program to remove multiple elements from python list

my_list = [4,16,17,32,40,15,18,19]
resultlist = [ item for item in my_list if item % 4!= 0]

print('list after removing elements =\n',resultlist)

Output

list after removing elements =
 [17, 15, 18, 19]

4. List Append() method to Remove multiple elements


Here we are looping over the elements of our list to check the elements of the list we want to delete elements of and appending them to result list.

  • We have our list ‘my_list’ which contains all elements
  • we have a second list ‘item_to_remove‘ which has elements that we want to delete.
  • So we are iterating on list(my_list) and matching item_to_remove elements. If the element does not exist then we append it to result_list‘.

This ‘result_list’ will contain an element that is not part of the item_to_remove list.

my_list = [4,16,17,32,40,15,18,19]
item_to_remove = [17,16,19]
result_list = []
for item in my_list:
  if item not in item_to_remove:
      result_list.append(item)

print('list after removing elements =\n',result_list)

Output

list after removing elements =
 [4, 32, 40, 15, 18]

5. List comprehension to Remove list of elements


In this program example, we are checking the list (item_to_remove) elements that are not present in the original list(my_list). It is just simple filtering based by using List comprehension with ‘if’ statement and not ‘in’ operator.

Program example


my_list = [4,16,17,32,40,15,18,19,67,65]

# list of item that to remove

item_to_remove = [17,15,19,67]

resultlist = [ item for item in my_list  if item not in item_to_remove]

print('list after removing elements =\n',resultlist)

Output

list after removing elements =
 [4, 16, 32, 40, 18, 65]

6. Remove mutiple elemnst from list when indexes are know


This is a simple approach when indexes of the items know to us we are passing the index of items using del keyword to delete it from the list.

#program to remove multiple elements from list python 

my_list = [4,16,17,32,40,15,18,19]

#index of item we want to delete

index_of_items = [1,4,6]

for item in sorted(index_of_items, reverse = True):  
  del my_list[item] 

print('list after removing elements =\n',my_list)

Output

list after removing elements =
 [4, 17, 32, 15, 19]

7. Remove multiple-element within range with del keyword


Similarly ‘del’ operator can be used to remove elements from the list by using indexes from start and stop range. We can specify the index as following to deleting elements from the list by using the del statement

#index range from start to an end index
del list[start:stop]

Let us understand with an example

my_list = [4,16,17,32,40,15,18,19]

#remove an element from index 1 to 3 using del statement

del my_list[1:4] 

print('list after removing elements =\n',my_list)

Output

list after removing elements =
 [4, 40, 15, 18, 19]

Conclusion

We have explored different ways to Remove multiple elements from python list with code examples. We can any of them as per your requirement.