 GeeksforGeeks App
Open App Browser
Continue

# 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``]` `# remove numbers using list comprehension``num_list ``=` `[i ``for` `i ``in` `num_list ``if` `i <``=` `100``]` `# display the list``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``]` `# traverse the numbers list``i``=``0``while` `i<``len``(num_list):``  ` `    ``# check if current number in the``    ``# list is greater than 100``    ``if` `num_list[i] > ``100``:``      ` `        ``# if true remove number from list``        ``num_list.remove(num_list[i])``    ``else``:``      ``i``+``=``1``      ``#increment of index value when item is not removed``        `  `# display the num_list after removing the``# values that are greater than 100``print``(num_list)`

Output

```[30, 65, 88, 98]
```

Time Complexity: O(n)
Auxiliary space: O(1)

## Python3

 `num_list ``=` `[``30``, ``200``, ``65``, ``88``, ``98``, ``500``, ``34``]` `num_list ``=` `list``(``filter``(``lambda` `x: x <``=` `100``, num_list))` `# display the num_list after removing the``# values that are greater than 100``print``(num_list)`

Output

```[30, 65, 88, 98, 34]
```

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

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)

#### 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``]` `# using enumerate() to loop over index and element``num_list ``=` `[num ``for` `i, num ``in` `enumerate``(num_list) ``if` `num <``=` `100``]` `# display the list``print``(num_list)``#This code is contributed by Vinay Pinjala.`

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

Method :using a for loop

This program removes all elements greater than a given value (100) from a list (num_list) by iterating over each element and appending the elements less than or equal to the given value to a new list (new_list). The new list is then printed.

## Python3

 `num_list ``=` `[``30``, ``200``, ``65``, ``88``, ``98``, ``500``, ``34``]``value ``=` `100` `new_list ``=` `[]``for` `num ``in` `num_list:``    ``if` `num <``=` `value:``        ``new_list.append(num)` `print``(new_list)`

Output

```[30, 65, 88, 98, 34]
```

The time complexity of this program is O(n), where n is the length of the original list.

The auxiliary space complexity of this program is also O(n), where n is the length of the original list.

My Personal Notes arrow_drop_up