Remove Minimum number from a list without min() or pop()
Question:
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):
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.
Answers:
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
Best pythonic way to do that without using pop() and min() functions:
def remove_minimum(values):
return sorted(values)[1:]
Or this version, if you want the list unchanged and care about the order:
def remove_minimum(values):
values.remove(sorted(values)[:1][0])
return values
you can return an orderer list and slice it, removing the first value.
def remove_min(values):
return sorted(values)[1:]
def remove_minimum(values):
values.remove(max(values, key=lambda x: -x))
return values
😀
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):
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.
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
Best pythonic way to do that without using pop() and min() functions:
def remove_minimum(values):
return sorted(values)[1:]
Or this version, if you want the list unchanged and care about the order:
def remove_minimum(values):
values.remove(sorted(values)[:1][0])
return values
you can return an orderer list and slice it, removing the first value.
def remove_min(values):
return sorted(values)[1:]
def remove_minimum(values):
values.remove(max(values, key=lambda x: -x))
return values
😀