Python has a min() built-in function:
Copy>>> darr = [1, 3.14159, 1e100, -2.71828]
>>> min(darr)
-2.71828
Answer from Greg Hewgill on Stack OverflowPython has a min() built-in function:
Copy>>> darr = [1, 3.14159, 1e100, -2.71828]
>>> min(darr)
-2.71828
If you want to use numpy, you must define darr to be a numpy array, not a list:
Copyimport numpy as np
darr = np.array([1, 3.14159, 1e100, -2.71828])
print(darr.min())
darr.argmin() will give you the index corresponding to the minimum.
The reason you were getting an error is because argmin is a method understood by numpy arrays, but not by Python lists.
Is there a way to use Python's min function to find the min in a subset of a list instead of the entire list?
Find the index of the minimum value in a list?
min() vs 'sort() and then list[0]'
As others have mentioned, min will be quicker. min just has to walk through the list once, whereas sort must do more compares, making it more complex.
However, one case you might want to use sort is when you need more than just the minimum. Eg. you want the 5 smallest items, not just the smallest. Here "sorted(mylist)[:5]" will do the job, but another option to keep in mind is the heapq module, as "heapq.nsmallest(5, mylist)" will often perform better than sort, at least for a small number of items.