• Register
1 vote

Problem :

I am not completely expert in Python, but still I am willing to learn good practices while I develop my skills.

I am trying to remove the high and low values from the list of numbers, I know how to do this , but I want to know the better way to do this.

myrawlist = [1, 4, 0, 3, 2]
mysortedlist.sort() #[0, 1, 2, 3, 4]
mytrimmedlist = mylist[1:-1] #[1, 2, 3]

I get the required output, but looking for the better way to do this.

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Please find below the another way to do it :

myrawlist = [1, 4, 0, 3, 2]

It assumes that the high or low do not have any duplicates in the list.

This will need to do for 2-4 passes through the given list: two to find your max and min values, and another up to 2 to find your values to remove if at all they both happen to be at the end of the list. You could easily reduce this to one by just writing the Python loop to find the required max and min in a single pass and also remember that the index of each so that you can later delete the items by index after the loop.

38.6k points