array min
Builtin
Finding the minimum value in a list is easily done using the builtin min()
1min([0,1,0,2,1,0,1,3,2,1,2,1]) # 0
Reduce
This is very similar to using reduce
:
1from functools import reduce
2A = [0,1,0,2,1,0,1,3,2,1,2,1]
3m = reduce(lambda a, x: min(a, x), A)
4print(m)
Output:
10
Iterative
However, very often you'll want to do so iteratively, to use the min up until this v
for example.
1A = [3,2,1,1,2,1,0,1,3,2,1,2,1]
2m = float('inf')
3for v in A:
4 m = min(v, m)
5 print(m, end=' ')
Output:
13 2 1 1 1 1 0 0 0 0 0 0 0
Notice the use of float('inf')
. It can feel a little counterintuitive mixing types, but this is easier to remember than sys.maxsize
.
See Also
[[array max]]