How to Find the Minimum or Maximum Value of an Array in Python



Python


In this article, we show how to find the minimum or maximum value of an array in Python.

This is important when you want to find the lowest or the greatest value of all values of an array in Python.

So we can find the minimum value of an array in Python using the min() function.

And we can find the maximum value of an array in Python using the max() function.

Let's see how to do this using an example shown below.



So, first, we must import numpy as np.

We then create a variable named randnums and set it equal to, np.random.randint(1,101,5)

This produces an array of 5 integers in which we can select from integers 1 to 100.

We then show the contents of the randnums variable.

You see that we get an array of 5 random numbers. These numbers that I got are 32,39,18, 52 and 20.

We then use the max() function on the randnums variable.

As the maximum value, we get returned 52, which is the largest number in the array.

If you want the position, or index, of the maximum value, the argmax() function is the function that gets the position, or index, of the maximum value of the array.

Remember that indexing starts at 0. Therefore, 52 is located at the index of 3.

We then use the min() function on the randnums variable.

As the minimum value, we get returned 18, which is the smallest number in the array.

If you want the position, or index, of the minimum value, the argmin() function is the function that gets the position, or index, of the minimum value of the array.

Therefore, 18 is located at the index of 2.

And this is all that is required to find the minimum or maximum value of an array in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...