Remove elements larger than a specific value Using Numpy

Python3




# Remove list elements greater than a given value using numpy
import numpy as np
  
num_list = [30, 200, 65, 88, 98, 500, 34]
  
# convert list to numpy array
arr = np.array(num_list)
  
# remove numbers greater than 100
new_arr = arr[arr <= 100]
  
# display the new_arr after removing the
# values that are greater than 100
print(new_arr)
 
#This code is contributed by Edula Vinay Kumar Reddy


Output:

[30 65 88 98 34]

Time complexity: O(n) 
Auxiliary Space: O(1)

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.

Similar Reads

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....

Remove list elements greater than a given value using remove() method

...

Remove list elements greater than a given value using the filter() method and lambda function

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....

Remove elements larger than a specific value Using Numpy

...

Remove elements larger than a specific value Using enum()

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....