Last updated: Apr 10, 2024
Reading timeยท5 min
To find the largest number in a list without using the max()
function:
None
.for
loop to iterate over the list.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 same approach can be used to find the minimum value in a list without using
the min()
function.
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
We used a for loop to iterate over the list.
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.
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 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
.
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:
for
loop to iterate over the list.min
or greater than max
.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 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.
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:
minimum
variable stores the smallest number in the listmaximum
variable stores the largest number in the listAlternatively, you can use the sorted()
function.
sorted()
This is a three-step process:
sorted()
function to get a new sorted list.-1
.-1
stores the largest number in the sorted list.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 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:
sorted()
function to get a new sorted list.0
to get the min value.-1
to get the max value.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 sorted() function returns a new sorted list from the items in the iterable.
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: