• Register
1 vote
878 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

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]
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 28 views
28 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, 2020 LizzyM 6.1k points
0 votes
1 answer 2 views
2 views
Problem: HI KODLOGS: I want to understand how to check if list is eampty in python please explain in detail with examples. For example, if passed the following: a = [] How do I check to see if a is empty?
asked 1 day ago Mashhoodch 6.7k points
0 votes
1 answer 23 views
23 views
Problem: I am a new student in learning python and i am not sure how to check if a list is empty in python. For example, if the following is passed: a = [] How to check if it is empty?
asked Nov 5, 2020 Han Li 710 points
2 votes
1 answer 22 views
22 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, 2020 Gavin 15.3k points
1 vote
1 answer 43 views
43 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, 2020 samhaz 5k points
1 vote
1 answer 52 views
52 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, 2020 LizzyM 6.1k points
0 votes
1 answer 23 views
23 views
I'm using Python's max function to find the largest integer in a dictionary called count, and the corresponding key (not quite sure if I'm saying it properly; my code probably explains itself better than I'm explaining it). The dictionary count is along the lines of {'a': 100, 'b': 210}, and ... there? If I had {'a': 120, 'b': 120, 'c': 100}, this would only find the first of a and b, not both.
asked Sep 13, 2020 NguyenTram 1k points
1 vote
1 answer 37 views
37 views
Problem: I am having an issue with removing the last element of a Python list. I have created a Python list with some items: is_a_list = [1, 2, 4, 6, 8, 0] And now, a situation has appeared in my program where I am in need to erase ‘0’, which ... disturbance with my existing list? I’d love to hear from you, guys. Would you please share a tip with me? Thanks a lot for your help.
asked Jul 5, 2020 adamSw 11.3k points
0 votes
1 answer 28 views
28 views
Problem: Hello Kodlogs, I am a high school standard student. Nowadays, I am learning computer programming using the Python programming language. Anyway, I just completed reading the Python list topic and learned what is a Python list and what to do with it! I’ve ... a professional way. Could you please suggest a better way to do such things? I am looking for your answers. Thanks, Mate.
asked Jul 2, 2020 adamSw 11.3k points
1 vote
1 answer 26 views
26 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, 2020 adamSw 11.3k points