Python max() and min(): Find max and min in Array

Python provides two handy functions, max() and min(), that makes it easy to find the largest (or the smallest) item in any iterable (e.g. list, set or array) of comparable elements.

In this beginner-friendly guide, we’ll explore how to use max() and min() effectively.

numbers = [3, 7, 1, 9, 4, 2]

max_number = max(numbers)  # 9

min_number = min(numbers)  # 1

1. Python max() Function

The max() function finds the maximum value in an iterable. It works with various data types, including numbers, strings, and more complex objects.

max_value = max(iterable)

The max() function is used to:

  • Compute the maximum of the values passed in its argument.
  • Lexicographically largest value if strings are passed as arguments.

Let’s explore some examples to understand how it works:

1.1. Find the Largest Integer in Array

In the following example, max() is applied to a list of numbers, and it returns the largest value, which is 9.

numbers = [3, 7, 1, 9, 4, 2]

max_number = max(numbers)

print(max_number)  # Output: 9

1.2. Find the Maximum String

In this example, max() is used with the key argument to find the longest string in the list of fruits.

fruits = ["apple", "banana", "cherry", "date"]

longest_fruit = max(fruits, key=len)

print(longest_fruit)  # Output: "banana"

1.3. Find Max Key or Value in a Dictionary

In this example, we are using the max() function to find the max key (lexicographically) and max value from a dictionary type.

prices = {
   'how': 45.23,
   'to': 612.78,
   'do': 205.55,
   'in': 37.20,
   'java': 10.75
}

maxKey = max( prices.keys() )
print(maxKey)  # to

maxVal = max( prices.values() )
print(maxVal)  # 612.78

2. Python min() Function

The min() function is similar to max(), but it finds the minimum value in an iterable. Its syntax is identical to that of max().

min_value = min(iterable)

This function is used to –

  • compute the minimum of the values passed in its argument.
  • lexicographically smallest value if strings are passed as arguments.

Let’s look at some examples.

2.1. Find the Lowest Integer in Array

When we run the previous example again with min(), it identifies the smallest number, which is 1.

numbers = [3, 7, 1, 9, 4, 2]

min_number = min(numbers)

print(min_number)  # Output: 1

2.2. Find the Smallest String in Array

Just like with max(), min() can be used with the key argument to find the shortest string.

fruits = ["apple", "banana", "cherry", "date"]

shortest_fruit = min(fruits, key=len)

print(shortest_fruit)  # Output: "date"

2.3. Find Min Key or Value in a Dictionary

Rerun the previous example with min() function.

prices = {
   'how': 45.23,
   'to': 612.78,
   'do': 205.55,
   'in': 37.20,
   'java': 10.75
}

minKey = min( prices.keys() )
print(minKey)  # do

minVal = min( prices.values() )
print(minVal)  # 10.75

3. Conclusion

The max() and min() functions in Python are powerful tools for finding the maximum and minimum values within arrays and other iterable objects. They are straightforward to use and provide valuable functionality for various programming tasks.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.