Remove Max and Min values from python list of integers Remove Max and Min values from python list of integers python python

Remove Max and Min values from python list of integers


Here's another way to do it if you don't want to change the order of the items:

mylist = [1, 4, 0, 3, 2]mylist.remove(max(mylist))mylist.remove(min(mylist))

Assumes that the high/low don't have any duplicates in the list, or if there are, that it's OK to remove only one of them.

This will need to do 2-4 passes through the list: two to find the max and min values, and up to 2 to find the values to remove (if they both happen to be at the end of the list). You could reduce this to one by writing a Python loop to find the max and min in a single pass (and remember the index of each so you can delete the items by index after the loop). However, min() and max() are implemented in C, so replacing them with Python code would probably result in lower performance even if it allowed you to reduce the number of passes.