Remove elements larger than a specific value from a list in Python
In this article, we will learn to remove elements from a list that is larger than a specific value in Python.
Example
Input: [12, 33, 10, 20, 25], value = 21
Output: [12, 10, 20]
Explanation: Removed all element from the list that are greater than 21.
Remove list elements greater than a given value using list comprehension
In the following example, we declare and assign a list of numbers to the variable num_list. With list comprehension, we can traverse each element from a list and perform an action on it. Here, we will check if the current number is less than or equal to 100. If true it will be returned as a list. We will assign the returned list to the same variable num_list.
Python3
num_list = [ 30 , 200 , 65 , 88 , 98 , 500 , 34 ]
num_list = [i for i in num_list if i < = 100 ]
print (num_list)
|
Output
[30, 65, 88, 98, 34]
Time Complexity: O(n)
Auxiliary Space: O(1)
Remove list elements greater than a given value using remove() method
In the following example, we will declare and assign a list num_list with numbers. We will remove numbers that are greater than 100 from the num_list. We will traverse each number from the list and check if the current number is greater than 100, if true we will remove it from the list using the Python remove() method. We will remove all values that are greater than the given number once we traverse the list.
Python3
num_list = [ 30 , 200 , 65 , 88 , 98 , 500 , 102 ]
i = 0
while i< len (num_list):
if num_list[i] > 100 :
num_list.remove(num_list[i])
else :
i + = 1
print (num_list)
|
Time Complexity: O(n)
Auxiliary space: O(1)
Remove list elements greater than a given value using the filter() method and lambda function
The code filters num_list using a lambda function to keep only values less than or equal to 100. It removes elements greater than 100 and displays the updated list.
Python3
num_list = [ 30 , 200 , 65 , 88 , 98 , 500 , 34 ]
num_list = list ( filter ( lambda x: x < = 100 , num_list))
print (num_list)
|
Output
[30, 65, 88, 98, 34]
Time complexity: O(n)
Auxiliary Space: O(1)
Remove elements larger than a specific value Using Numpy
Python3
import numpy as np
num_list = [ 30 , 200 , 65 , 88 , 98 , 500 , 34 ]
arr = np.array(num_list)
new_arr = arr[arr < = 100 ]
print (new_arr)
|
Output:
[30 65 88 98 34]
Time complexity: O(n)
Auxiliary Space: O(1)
Remove elements larger than a specific value Using enum()
In this code, we use the enumerate() function to loop over the index and element of the num_list. Then, we filter the numbers using list comprehension just like before. The enumerate() function provides an elegant way to loop over the index and element of a list simultaneously, and it can help to make the code more readable.
Python3
num_list = [ 30 , 200 , 65 , 88 , 98 , 500 , 34 ]
num_list = [num for i, num in enumerate (num_list) if num < = 100 ]
print (num_list)
|
Output
[30, 65, 88, 98, 34]
The time complexity of the code is O(n) as the list comprehension and enumeration loop both iterate over the entire list once, where n is the length of the input list.
The auxiliary space is also O(n) as a new list is created to store the filtered elements, which can be at most the size of the original list.
RECOMMENDED ARTICLES – Remove the given element from the list
Last Updated :
27 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...