Skip to content

How to Remove Maximum and Minimum Values from a List in Python

How to remove the highest and lowest values from a Python list using max(), min(), list comprehensions, and in-place methods, with simple examples and tips for handling duplicates.

This guide explains how to remove the maximum and minimum values from a list in Python. We'll cover both modifying the list in-place and creating a new list without the extreme values, using built-in functions like max(), min(), remove(), and list comprehensions.

Removing Max and Min In-Place (Modifying the Original List)

You can remove the maximum and minimum values directly from the original list using max(), min(), and remove():

my_list = [1, 25, 50, 100]

my_list.remove(max(my_list))
print(my_list)  # Output: [1, 25, 50]

my_list.remove(min(my_list))
print(my_list)  # Output: [25, 50]
  • max(my_list): Returns the largest item in the list.
  • min(my_list): Returns the smallest item in the list.
  • my_list.remove(value): Removes the first occurrence of value from my_list. The list is modified in-place.

Handling Empty Lists

max() and min() raise a ValueError if you call them on an empty list. Always check for emptiness before calling these functions if there's a possibility the list might be empty:

my_list = []


try:
    my_list.remove(max(my_list))
except ValueError as e:
    print(e) # list.remove(x): x not in list

if my_list:  # Check if the list is NOT empty
    my_list.remove(max(my_list))
    my_list.remove(min(my_list))
  • The if my_list checks if the list is truthy, i.e. not empty.

Often, you'll want to create a new list without the maximum and minimum values, leaving the original list unchanged. List comprehensions are ideal for this:

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)  # Output: [25, 50]
print(my_list)   # original list is unchanged
  • [number for number in my_list if ... ]: This is a list comprehension.
  • if number > min(my_list) and number < max(my_list): This condition keeps only the numbers that are strictly greater than the minimum and strictly less than the maximum.
  • This approach is generally preferred because it creates a new list without changing the original one.
Navigation