Question

I need to know how the min function of python works if there are more than one item in a list which are minimum. Which one takes min? Say A = [5, 3, 1, 4, 1]. Now if I say A.remove(min(A)) which one will be removed? The first 1 or the second 1?

Was it helpful?

Solution

In this case it will remove the first one. It's more about the behavior of the list.remove function rather than the min function in this case. min just returns the lowest value in the list so it returns the integer 1. list.remove removes the leftmost instance of the parameter passed to it.

Also, as answered in this answer to the question that mgilson linked above, if you are dealing with objects rather than values (i.e. lists rather than integers), the first one matching the minimum value will be selected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top