Last updated: Apr 9, 2024
Reading timeยท3 min
To remove the max and min numbers from a list:
max()
and min()
functions to get the max and min values in the
list.list.remove()
method to remove the max and min numbers from the
list.my_list = [1, 25, 50, 100] # โ Remove max value from list my_list.remove(max(my_list)) print(my_list) # ๐๏ธ [1, 25, 50] # ----------------------------------- # โ Remove min value from list my_list.remove(min(my_list)) print(my_list) # ๐๏ธ [25, 50]
The max() function returns the largest item in an iterable or the largest of two or more arguments.
my_list = [1, 25, 50, 100] result = max(my_list) print(result) # ๐๏ธ 100
The function takes an optional default
keyword argument which is used to
specify a value to return if the provided iterable is empty.
result = max([], default=0) print(result) # ๐๏ธ 0
default
keyword argument is not provided, the function raises a ValueError
.The min function returns the smallest item in an iterable or the smallest of two or more arguments.
The list.remove() method removes the first item from the list whose value is equal to the passed-in argument.
The remove()
method mutates the original list and
returns None.
If you want to remove the max and min numbers from a list without changing the original list, create a copy.
my_list = [1, 25, 50, 100] list_copy = my_list.copy() # โ Remove the max value from the list list_copy.remove(max(list_copy)) print(list_copy) # ๐๏ธ [1, 25, 50] # ----------------------------------- # โ Remove the min value from the list list_copy.remove(min(list_copy)) print(list_copy) # ๐๏ธ [25, 50]
The list.copy() method returns a shallow copy of the object on which the method was called.
We created a copy, so we can remove the max and min numbers from the copy and not from the original list.
You will get a ValueError
if the list you pass to the max
or min
function
is empty.
Use a try/except statement if you need to handle the error.
my_list = [] # โ Remove max value from list try: my_list.remove(max(my_list)) print(my_list) # ๐๏ธ [1, 25, 50] except ValueError: pass # ----------------------------------- # โ Remove min value from list try: my_list.remove(min(my_list)) print(my_list) # ๐๏ธ [25, 50] except ValueError: pass
The max
and min
functions raise a ValueError
when passed an empty
iterable.
The list in the example is empty, so the except
block runs.
The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.
You can also use a list comprehension to remove the max and min numbers from a list.
my_list = [1, 25, 50, 100] new_list = [ number for number in my_list if number > min(my_list) and number < max(my_list) ] print(new_list) # ๐๏ธ [25, 50]
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
On each iteration, we check if the current number is greater than the min
value and less than the max
value and return the result.
The list comprehension will return a new list that doesn't contain the max and min values from the original list.
The same approach can be used to remove only the min value.
my_list = [1, 25, 50, 100] new_list = [ number for number in my_list if number < max(my_list) ] print(new_list) # ๐๏ธ [1, 25, 50]
We only check if the number is greater than min
.
If you only need to remove the max
value, check if the number is less than
max
.
You can learn more about the related topics by checking out the following tutorials: