Open In App

Remove multiple elements from a list in Python

Last Updated : 29 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 a different scenarios.

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]

Time Complexity: O(n)
Auxiliary Space: O(n)

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 23

Time Complexity: O(n)
Auxiliary Space: O(n)

Example #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 50

Time Complexity: O(n)
Auxiliary Space: O(n)

Example #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]

Time Complexity: O(n)
Auxiliary Space: O(n)

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 50

Method: Using enumerate function

Python3




list1 = [11, 5, 17, 18, 23, 50]
 
list1 = [ elem for i,elem in enumerate(list1) if elem % 2 != 0]
 
print(list1)


Output

[11, 5, 17, 23]

Time Complexity: O(n log n)
Auxiliary Space: O(n)

Approach using numpy.delete():

Algorithm:

Create a numpy array from the given list.
Find the indices of the elements to be removed.
Use numpy.delete() to remove the elements based on the indices found in the previous step.
Convert the resulting numpy array back to a list.
Code:

Python3




import numpy as np
 
#creating a list
list1 = [12, 15, 3, 10]
 
#convert list to numpy array
arr = np.array(list1)
 
#indices of elements to be removed
remove_idx = [0, 2]
 
#use numpy.delete() to remove elements
new_arr = np.delete(arr, remove_idx)
 
#convert numpy array back to list
new_list = new_arr.tolist()
 
#print the results
print("Removed =", [list1[i] for i in remove_idx], ", New_List =", new_list)


Output;

Removed = [12, 3] , New_List = [15, 10]

Time complexity: O(n)
Space complexity: O(n) (for creating the numpy array)



Previous Article
Next Article

Similar Reads

Python - Remove multiple elements from Set
Given a set, the task is to write a Python program remove multiple elements from set. Example: Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [2, 4, 8] Output : {9, 6, 7} Explanation : 2, 4 are removed from set. Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [4, 8] Output : {2, 9, 6, 7} Explanation : 4 is removed from set. Method #1 : Using "-" opera
4 min read
Python - Ways to remove multiple empty spaces from string List
Sometimes, while working with Python strings, we have a problem in which we need to perform the removal of empty spaces in Strings. The problem of filtering a single space is easier. But sometimes we are unaware of the number of spaces. This has applications in many domains. Let's discuss certain ways in which this task can be performed. Method #1
6 min read
Python | Remove multiple keys from dictionary
While working with Python dictionaries, we can have a utility in which we require to remove more than one key at once. This type of problem can occur while working in the Web Development domain with NoSQL Databases. Let's discuss certain ways in which this task can be performed. Remove multiple keys from a dictionary using del Here, we are using a
6 min read
Get Index of Multiple List Elements in Python
In Python, retrieving the indices of specific elements in a list is a common task that programmers often encounter. There are several methods to achieve this, each with its own advantages and use cases. In this article, we will explore some different approaches to get the index of multiple list elements in Python. Get Index Of Multiple List Element
3 min read
Python | Remove last K elements of list
We often come to situations in which we need to decrease the size of the list by truncating the k last elements of the list. This has its application in the day-day programming when sometimes we require getting all the lists of similar size or removing the last few records from a list. Let's discuss a few ways in which this task can be performed. U
6 min read
Python | Remove unordered duplicate elements from a list
Given a list, the task is to remove the duplicate elements. All the elements which are not in same order but made of same characters/numbers are considered as duplicates. Examples: Input : ['gfg', 'ggf', 'fgg', 'for', 'orf', 'ofr', 'rfo', 'rof', 'fro'] Output : ['for', 'fgg'] Input: ['110', '101', '001', '010', '100'] Output: ['001', '011'] Method
2 min read
Python | Remove trailing empty elements from given list
Given a list, the task is to remove trailing None values from last of the list. Let's discuss a few methods to solve the given task. Examples: Input: [1, 2, 3, 4, None, 76, None, None] Output: [1, 2, 3, 4, None, 76] Input: [1, 2, None, None, None, None, None, 5] Output: [1, 2, None, None, None, None, None, 5] Method #1: Using naive method C/C++ Cod
7 min read
Python | Remove elements of list that are repeated less than k times
Given a list of integers (elements may be repeated), write a Python program to remove the elements that are repeated less than k times. Examples: Input : lst = ['a', 'a', 'a', 'b', 'b', 'c'], k = 2Output : ['a', 'a', 'a', 'b', 'b'] Input : lst = [1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4], k = 3Output : [1, 1, 1, 1, 3, 3, 3] Approach #1: Pythonic naive Count
4 min read
Python | Remove List elements containing given String character
Sometimes, while working with Python lists, we can have problem in which we need to perform the task of removing all the elements of list which contain at least one character of String. This can have application in day-day programming. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is brute force way in w
7 min read
Python - Remove Negative Elements in List
Sometimes, while working with Python lists, we can have a problem in which we need to remove all the negative elements from list. This kind of problem can have application in many domains such as school programming and web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [6, 4, 3] Output : [6, 4, 3] I
5 min read