In this article we will discuss different ways to remove multiple elements from list.
Suppose we have a list of numbers i.e.
# List of Numbers listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
Now we want to remove all the numbers from list, which are multiple of 3.
Remove multiple elements from list while Iterating
Iterate over the list and remove them one by one if its divisible by 3 i.e.
# Remove all numbers from list which are divisible by 3 for elem in list(listOfnum): if elem % 3 == 0: listOfnum.remove(elem)
Contents of the list will be now,
[44, 56, 34, 4, 44]
Remove multiple elements from list using List Comprehension
Same thing can be achieved by List Comprehension i.e.
# Remove all numbers from list which are divisible by 3 listOfnum = [ elem for elem in listOfnum if elem % 3 != 0]
It will basically create a new list out of the existing list. But new list will contain the elements only which are not multiple of 3. Then replace the existing list with new one. So, it will also remove all the multiple of 3 from the list i.e.
Frequently Asked:
[44, 56, 34, 4, 44]
Remove Multiple elements from list by index range using del
Suppose we want to remove multiple elements from a list by index range, then we can use del keyword i.e.
del <list>[<index1>:<index2>]
It will delete the elements in list from index1 to index2 – 1.
For example,
We have a list of int i.e.
# List of Numbers listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
Let’s remove elements in list from index 1 to 3 i.e.
# Removes elements from index 1 to 3 del listOfnum[1:4]
Lists contents will be now,
[12, 34, 3, 4, 33, 44]
Complete example is as follows,
def main(): print("***** Remove multiple elements from list while Iterating *****") # List of Numbers listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44] print("Original List : " , listOfnum) # Remove all numbers from list which are divisible by 3 for elem in listOfnum: if elem % 3 == 0: listOfnum.remove(elem) print("Modified List : " , listOfnum) print("***** Remove multiple elements from list using List Comprehension *****") # List of Numbers listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44] print("Original List : " , listOfnum) # Remove all numbers from list which are divisible by 3 listOfnum = [ elem for elem in listOfnum if elem % 3 != 0] print("Modified List : " , listOfnum) print("***** Remove multiple elements from list by index range using del *****") # List of Numbers listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44] print("Original List : " , listOfnum) # Removes elements from index 1 to 3 del listOfnum[1:4] print("Modified List : " , listOfnum) if __name__ == '__main__': main()
Output:
***** Remove multiple elements from list while Iterating ***** Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44] Modified List : [44, 56, 34, 4, 44] ***** Remove multiple elements from list using List Comprehension ***** Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44] Modified List : [44, 56, 34, 4, 44] ***** Remove multiple elements from list by index range using del ***** Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44] Modified List : [12, 34, 3, 4, 33, 44]
Hello!
I am getting a problem while removing elements from the list.I have tried this with multiple ways like indexing and verifying elements in a list.
Example-
a=[4,2,5,7,4,65,5,5,54,5,5,5,5,5]
I want ro remove all the 5s in from the list but unable to do so.
Can anyone help me out?
mod_list = [e for e in a if e != 5]
mod_list will have all elements except 5.