Remove multiple elements from a list in Python
Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition.
Example:
Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17, 18, 23, 50] Output: Remove = [1:5], New_list = [11, 50]Multiple elements can be deleted from a list in Python, based on the knowledge we have about the data. Like, we just know the values to be deleted or also know the indexes of those values. Let’s see different examples based on different scenario.
Example #1: Let’s say we want to delete each element in the list which is divisible by 2 or all the even numbers.
Python3
# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # Iterate each element in list # and add them in variable total for ele in list1: if ele % 2 == 0: list1.remove[ele] # printing modified list print["New list after removing all even numbers: ", list1] |
Output:
New list after removing all even numbers: [11, 5, 17, 23]
Example #2: Using list comprehension
Removing all even elements in a list is as good as only including all the elements which are not even[ i.e. odd elements].
Python3
# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # will create a new list, # excluding all even numbers list1 = [ elem for elem in list1 if elem % 2 != 0] print[*list1] |
Output:
11 5 17 23Example #3: Remove adjacent elements using list slicing
Below Python code remove values from index 1 to 4.
Python3
# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # removes elements from index 1 to 4 # i.e. 5, 17, 18, 23 will be deleted del list1[1:5] print[*list1] |
Output:
11 50Example #4: Using list comprehension
Let’s say the elements to be deleted is known, instead of the indexes of those elements. In this case, we can directly eliminate those elements without caring about indexes which we will see in next example.
Python3
# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # items to be removed unwanted_num = {11, 5} list1 = [ele for ele in list1 if ele not in unwanted_num] # printing modified list print["New list after removing unwanted numbers: ", list1] |
Output:
New list after removing unwanted numbers: [17, 18, 23, 50]Example #5: When index of elements is known.
Though indexes of elements in known, deleting the elements randomly will change the values of indexes. Hence, it is always recommended to delete the largest indices first. Using this strategy, index of smaller values will not be changed. We can sort the list in reverse order and delete the elements of list in descending order.
Python3
# Python program to remove multiple # elements from a list # creating a list list1 = [11, 5, 17, 18, 23, 50] # given index of elements # removes 11, 18, 23 unwanted = [0, 3, 4] for ele in sorted[unwanted, reverse = True]: del list1[ele] # printing modified list print [*list1] |
Output:
5 17 50Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article
Remove multiple elements from list while Iterating
Iterate over the list and remove them one by one if its divisible by 3 i.e.
Contents of the list will be now,
[44, 56, 34, 4, 44]
Remove Multiple Elements From a List in Python
Python Python List
Created: August-01, 2021 | Updated: October-02, 2021
To remove multiple values from a Python list, we can either remove the actual values of the list or the indexes of values to be removed from the list. We can use if...else control statements, list comprehension, list slicing, and for loops to remove multiple elements from a list in Python.
Remove multiple elements from a list in Python
In this tutorial, you will learn to remove multiple elements from a list in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. We will be discussing few in-built functions which can be used to remove elements from a list.
Elements in a list can be deleted based on certain conditions or by specifying the range of their indexes.
First, we will see how to delete elements based on a condition.
Condition: Numbers that are divisible by 5 should be deleted from the list.
For example, given a list of numbers, you have to remove all those elements from the list which are divisible by 5.
Input: [10, 3, 12, 15, 5, 8]
Output: [3, 12, 8]
To solve this problem, we can follow these approaches:
- For removing multiple elements from a list based on a certain condition, we can follow these approaches-
- Remove multiple elements from a list while Iterating.
- Remove multiple elements from a list using List Comprehension.
- For removing multiple elements from a list by index range we can use del operator.