Remove the Max and Min numbers from a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Remove the Max and Min numbers from a List in Python

To remove the max and min numbers from a list:

  1. Use the max() and min() functions to get the max and min values in the list.
  2. Use the list.remove() method to remove the max and min numbers from the list.
main.py
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]

remove max and min numbers from list

The code for this article is available on GitHub

The max() function returns the largest item in an iterable or the largest of two or more arguments.

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

main.py
result = max([], default=0) print(result) # ๐Ÿ‘‰๏ธ 0
If the iterable is empty and the 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.

# Removing the max and min numbers without changing the original list

If you want to remove the max and min numbers from a list without changing the original list, create a copy.

main.py
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]

removing max and min number without changing the original list

The code for this article is available on GitHub

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.

# Handling the case where the list is empty

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.

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

handling the case where the list is empty

The code for this article is available on GitHub

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.

# Remove the Max and Min numbers from a List using a list comprehension

You can also use a list comprehension to remove the max and min numbers from a list.

main.py
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]
The code for this article is available on GitHub

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.

main.py
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]
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev