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.

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):
   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

Leave a Reply Cancel reply