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

Borislav Hadzhiev

Last updated: Apr 10, 2024

Reading timeยท5 min

- Find the largest number in a list without max() in Python
- Find the Min and Max in a List without min/max functions
- Find the largest number in a list without max() using sorted()
- Find the Min and Max in a List without min/max using sorted()

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

- Declare a new variable and initialize it to
`None`

. - Use a
`for`

loop to iterate over the list. - Check if each number is greater than the current max value.
- 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`

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`

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`

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`

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.

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

- Initialize two variables that will store the min and max values.
- Use a
`for`

loop to iterate over the list. - Check if each number is less than
`min`

or greater than`max`

. - 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]`

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.

`sorted()`

This is a three-step process:

- Use the
`sorted()`

function to get a new sorted list. - Access the list item at index
`-1`

. - 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`

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`

.

This is a three-step process:

- Use the
`sorted()`

function to get a new sorted list. - Access the list item at index
`0`

to get the min value. - 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.

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

- Find the indices of duplicate items in a List in Python
- Find elements in one List that are not in the other (Python)
- Find common values in multiple Lists in Python
- Find Min and Max values in Tuple or List of Tuples in Python
- Find the most/least common element in a List in Python
- Find object(s) in a List of objects in Python
- Find second Smallest or Largest number in a List in Python
- Check if a List is Sorted (ascending/descending) in Python