Remove multiple items from list Python
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. Show
1. Using Python SETin 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
#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
#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 conditionHere 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 elementsHere 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.
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 elementsIn 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 knowThis 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 keywordSimilarly 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]
ConclusionWe have explored different ways to Remove multiple elements from python list with code examples. We can any of them as per your requirement. Related Posts:
Powered by Contextual Related Posts |