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. Python SET to Remove multiple elements from python list
In this example python 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 given list mylist to a SET.
- We have converted the list list_item_to_remove that contains item to removed into SET.
- In the result_lst, we found the difference between these two sets and it contain 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, We are 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 remove that element from the list using the list.remove() method and 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
In this python program example, we are using list comprehension to delete multiple items from the list that are not divisible by 4. The resultlist 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
In this python code example, we are iterating through elements of a given list to find the elements that are not exits in item_to_remove list.
- We have given list ‘my_list’ which contains all elements.
- we have a second list ‘item_to_remove‘ based on this we have to delete form my_list.
- we are iterating through list(my_list) and appending item into result_list‘.that are not in item_to_remove list .
This ‘result_list’ will contain an element that does not exist in 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 follows 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 how to Remove multiple elements from python list with code examples. We can any of them as per your requirement.