Find Max and Min in List without max() and min() in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Find the largest number in a list without max() in Python
  2. Find the Min and Max in a List without min/max functions
  3. Find the largest number in a list without max() using sorted()
  4. Find the Min and Max in a List without min/max using sorted()

# Find the largest number in a list without max() in Python

To find the largest number in a list without using the max() function:

  1. Declare a new variable and initialize it to None.
  2. Use a for loop to iterate over the list.
  3. Check if each number is greater than the current max value.
  4. Assign each number that meets the condition to the new variable.
main.py
my_list = [2, 8, 4, 1, 7] largest_number = None for number in my_list: if largest_number is None or largest_number < number: largest_number = number # โœ… get the largest number print(largest_number) # ๐Ÿ‘‰๏ธ 8 # โœ… get the index of the largest number print(my_list.index(largest_number)) # ๐Ÿ‘‰๏ธ 1

find largest number in list without max

The code for this article is available on GitHub

The same approach can be used to find the minimum value in a list without using the min() function.

main.py
my_list = [4, 3, 7, 1, 9] min_number = None for number in my_list: if min_number is None or number < min_number: min_number = number # โœ… get the min value in a list print(min_number) # ๐Ÿ‘‰๏ธ 1 # โœ… get the index of the min value print(my_list.index(min_number)) # ๐Ÿ‘‰๏ธ 3

find minimum value in list without min function

The code for this article is available on GitHub

We used a for loop to iterate over the list.

If the largest_number variable stores a None value or is less than the current number, we reassign the variable to the number of the current iteration.

After the last iteration, the largest_number variable stores the max value in the list.

If you need to find the index of the largest number, use the list.index() method.

main.py
my_list = [2, 8, 4, 1, 7] largest_number = None for number in my_list: if largest_number is None or largest_number < number: largest_number = number print(largest_number) # ๐Ÿ‘‰๏ธ 8 index_largest_number = my_list.index(largest_number) print(index_largest_number) # ๐Ÿ‘‰๏ธ 1

find index of largest number in list

The code for this article is available on GitHub

The list.index() method returns the index of the first item whose value is equal to the provided argument.

If the list is empty, the largest_number variable remains set to None.

main.py
my_list = [] largest_number = None for number in my_list: if largest_number is None or largest_number < number: largest_number = number print(largest_number) # ๐Ÿ‘‰๏ธ None index_largest_number = ( my_list.index(largest_number) if largest_number is not None else None ) print(index_largest_number) # ๐Ÿ‘‰๏ธ None

if list is empty largest number will be none

We also used an inline if/else statement when calling the list.index() method because the method raises a ValueError if there is no such item in the list.

Here is a reusable function that can be used to find the min and max values in a list without the min() and max() functions.

# Find the Min and Max in a List without min/max functions

To find the min and max values in a list without the min/max functions:

  1. Initialize two variables that will store the min and max values.
  2. Use a for loop to iterate over the list.
  3. Check if each number is less than min or greater than max.
  4. Update the values of the variables.
main.py
def get_min_max(l): if not l: raise ValueError('Please provide a non-empty list') minimum = maximum = l[0] for number in l[1:]: if number < minimum: minimum = number elif number > maximum: maximum = number return [minimum, maximum] print(get_min_max([2, 6, 4, 8])) # ๐Ÿ‘‰๏ธ [2, 8] print(get_min_max([6, 2, 3, 1])) # ๐Ÿ‘‰๏ธ [1, 6]

find min max in list without min max with reusable function

The code for this article is available on GitHub

The first step is to assign the first number in the list to the minimum and maximum variables.

We used a for loop to iterate over the rest of the list.

On each iteration, we check if the current number is less than the value stored in the minimum variable.

If the condition is met, we assign the number to the variable.

Otherwise, we check if the current number is greater than the value stored in the maximum variable.

If the condition is met, we assign the number to the variable.

After the last iteration:

  • the minimum variable stores the smallest number in the list
  • the maximum variable stores the largest number in the list

Alternatively, you can use the sorted() function.

# Find the largest number in a list without max() using sorted()

This is a three-step process:

  1. Use the sorted() function to get a new sorted list.
  2. Access the list item at index -1.
  3. The item at index -1 stores the largest number in the sorted list.
main.py
my_list = [2, 8, 4, 1, 7] largest_number = sorted(my_list)[-1] print(largest_number) # ๐Ÿ‘‰๏ธ 8 index_largest_number = my_list.index(largest_number) print(index_largest_number) # ๐Ÿ‘‰๏ธ 1

find largest number in list without max using sorted

The code for this article is available on GitHub

The sorted() function returns a new sorted list from the items in the iterable.

The list item at index -1 stores the largest number in the list.

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

# Find the Min and Max in a List without min/max using sorted()

This is a three-step process:

  1. Use the sorted() function to get a new sorted list.
  2. Access the list item at index 0 to get the min value.
  3. Access the list item at index -1 to get the max value.
main.py
def get_min_max(l): sorted_list = sorted(l) minimum = sorted_list[0] maximum = sorted_list[-1] return [minimum, maximum] print(get_min_max([2, 6, 4, 8])) # ๐Ÿ‘‰๏ธ [2, 8] print(get_min_max([6, 2, 3, 1])) # ๐Ÿ‘‰๏ธ [1, 6]
The code for this article is available on GitHub

The sorted() function returns a new sorted list from the items in the iterable.

main.py
print(sorted([2, 6, 4, 8])) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8] print(sorted([2, 6, 4, 8])[0]) # ๐Ÿ‘‰๏ธ 2 print(sorted([2, 6, 4, 8])[-1]) # ๐Ÿ‘‰๏ธ 8

We accessed the list item at index 0 to get the smallest number in the list.

The list item at index -1 stores the largest number in the list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev