# Python: Find the Min and Max in a Numeric List (4 Ways)

In this concise example-based article, we will go through some techniques to find the maximum and minimum elements in a numeric list (a list that contains only numbers) in Python. Besides that, we will also discuss the performance of each approach to find out which one is the fastest.

## Using the min() and max() function

The `min()` and `max()` functions take a list (or other iterable) as an argument and return the smallest and largest elements, respectively. You don’t have to import anything in order to call them.

Example:

``````numbers = [4.4, 5.5, 6.6, 1.1, 2.2, 3.3, 7.7, 9.9, 8.8]

min_number = min(numbers)
max_number = max(numbers)

print(f"Min number: {min_number}")
print(f"Max number: {max_number}")``````

Output:

``````Min number: 1.1
Max number: 9.9``````

These are built-in functions designed specifically for the purpose of finding min and max, so both their performance and syntax are great. In most cases, these functions are your best choice. The time complexity of these functions is `O(n)`, where `n` is the length of the list.

## Using a loop with comparison operators

The main idea of this approach is to use a loop to iterate over the elements of the list and compare them with two variables that store the current min and max values.

Example:

``````numbers = [4.4, 5.5, 6.6, 1.1, 2.2, 3.3, 7.7, 9.9, 8.8]

# Initialize two variables with some arbitrary values
min_number = float('inf')
max_number = float('-inf')

# Use a loop to iterate over the elements of the list
for x in numbers:
if x < min_number:
# If yes, update the min value with the new value
min_number = x
if x > max_number:
# If yes, update the max value with the new value
max_number = x

print(f"Min number: {min_number}")
print(f"Max number: {max_number}") ``````

Output:

``````Min number: 1.1
Max number: 9.9``````

The time complexity of this technique is `O(n)`, which means its performance is similar to the previous one. However, you have to write more code.

## Using the reduce() function

The `reduce()` function from the `functools` module brings to the table an alternative solution. This function takes a function and an iterable as arguments and applies the function cumulatively to the elements of the iterable, from left to right, and returns a single value. The code example below demonstrates how we can make use of it to get the min and max values from a numeric list:

``````from functools import reduce
numbers = [4.4, 5.5, 6.6, 1.1, 2.2, 3.3, 7.7, 9.9, 8.8]

min_number = reduce(lambda x, y: x if x < y else y, numbers)
max_number = reduce(lambda x, y: x if x > y else y, numbers)

print(f"Min number: {min_number}")
print(f"Max number: {max_number}") ``````

Output:

``````Min number: 1.1
Max number: 9.9``````

Like the two previous approaches, the time complexity of this method is `O(n)`, where `n` is the number of elements in the list. The performance is similar.

## Using the sorted() method

You can use the `sorted()` function to sort the elements of a list (or an iterable) in ascending order and returns a new sorted list. The min value will be the first element, and the max value will be the last element of the sorted iterable.

Example:

``````numbers = [4.4, 5.5, 6.6, 1.1, 2.2, 3.3, 7.7, 9.9, 8.8]

sorted_numbers = sorted(numbers)
min_number = sorted_numbers
max_number = sorted_numbers[-1]

print(f"Min number: {min_number}")
print(f"Max number: {max_number}")

end_time = time.time()``````

Output:

``````Min number: 1.1
Max number: 9.9``````

Sorting might be expensive when your list is extremely large. The time complexity of this method is `O(n log n)`, where `n` is the element count of the list.

## Conclusion

We’ve covered more than one way to determine the min and max of a list in Python. In general, the first approach should be used because it’s both concise and efficient.

Subscribe
Notify of 