• Register
1 vote
541 views

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.

6 5 3
7,540 points

1 Answer

0 votes

Solution :

Please find below the another way to do it :

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

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.

9 7 4
38,600 points

Related questions

1 vote
1 answer 19 views
19 views
Problem : I have started learning python a few days ago . So I am doing my practice and I am getting cofused with some little things. Supose I have a list that is: listOfnumbers = [16, 22, 96, 15, 4, 39, 56, 40, 13, 22, 15] So what should I have to do to remove element from list python by index
asked Apr 3 LizzyM 6.1k points
2 votes
1 answer 18 views
18 views
Problem: Let’s say I have a list x = [] in python. How could I check this list is empty or not? Thanks in advance.
asked Mar 13 Gavin 15.3k points
1 vote
1 answer 26 views
26 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16 samhaz 5k points
1 vote
1 answer 25 views
25 views
Problem: I am a begginer in progamming language. I have a qusestion which is: is the empty set an element of every set Please help me to understand this.
asked Mar 28 LizzyM 6.1k points
1 vote
1 answer 16 views
16 views
Problem: Hello guys, I found python list is fun. I was doing some operation with a python list. Let me say, I have a python list looks: is_a_list = [1, 5, 3, 6, 3, 5, 6, 1] with mixed of duplicate values in it. Now, I am willing to remove all the duplicate elements from the list. How do you do that in an easiest and efficient way? I am eagerly waiting for your answers. Thanks.
asked Jun 25 adamSw 11.3k points