Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove Minimum number from a list without min() or pop()

My problem is:
Write a function

def remove_minimum(values)
that returns the list resulting from removing the minimum value from a given list without using the min() function nor the pop() function.

Do not print the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have a code that sorts and reverses the list but I cannot figure out how to remove the minimum without using the pop() or min() function.

def remove_minimum (values):
   values.sort()
   values.reverse()
   
   return values

There is a problem on here already that I used to help me get to my code that I have right now but they use pop(). I tried to search how to make the pop() function but am struggling.

>Solution :

For this type of algorithm question, you can use simple looping to search and compare to get it:

This allows you to achieve the result without using pop or min or sort. IF you can use some of these it will be much simpler.


def remove_min(values):
    # search the minimum value in the list, starting with the first num
    min_val = values[0]
    
    for val in values[1:]:
        if val < min_val:
            min_val = val
    
    # remove the minimum value using List Comprehension as comments suggest.  
    return [x for x in values if x != min_val]
    


L = [9, 8, 3, 5, 6, 7]

print(remove_min(L))  # [9, 8, 5, 6, 7]   "3" is gone
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading