How to Find the Maximum Value in a List in Python
This tutorial will demonstrate how to find the maximum value in a list in Python.
This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays.
for
Finding the Maximum Value in a List
Using Loops in Python
A Python for
loop can find the maximum value in a list by comparing each value in the array and storing the largest value in a variable.
For example, let's declare an array of random integers and print the maximum value. Also, declare a variable max_value
to store the maximum value and print it after the loop is finished.
numbers = [55, 4, 92, 1, 104, 64, 73, 99, 20]
max_value = None
for num in numbers:
if max_value is None or num > max_value:
max_value = num
print("Maximum value:", max_value)
Output:
Maximum value: 104
If you need to further manipulate the output using the index of the maximum value, just use the Python function enumerate
, which allows for
the loop to accept a second argument representing the index of the current loop iteration.
Add a new variable max_idx
to store the maximum index of the maximum value.
numbers = [55, 4, 92, 1, 104, 64, 73, 99, 20]
max_value = None
max_idx = None
for idx, num in enumerate(numbers):
if max_value is None or num > max_value:
max_value = num
max_idx = idx
print("Maximum value:", max_value, "At index: ", max_idx)
Output:
Maximum value: 104 At index: 4
max()
Find the maximum value in a list
using the function in Python
Python has a predefined function called max()
, which returns the maximum value in a list.
Using max()
to find the maximum value only requires one line of code.
numbers = [55, 4, 92, 1, 104, 64, 73, 99, 20]
max_value = max(numbers)
print("Maximum value:", max_value)
Output:
Maximum value: 104
If you need the index of the maximum value, we can call the built-in function in Python lists index()
. This function will return the index of the specified element in the array.
numbers = [55, 4, 92, 1, 104, 64, 73, 99, 20]
max_value = max(numbers)
print("Maximum value:", max_value, "At index:", arr.index(max_value))
Output:
Maximum value: 104 At index: 4
max()
Finding the maximum value in a list of strings and dictionaries
using
The function max()
also provides support for string list and dictionary data types.
Given a list of strings, the function max()
will return the largest element, sorted alphabetically. The letter Z is the largest value, and A is the smallest value.
strings = ["Elephant", "Kiwi", "Gorilla", "Jaguar", "Kangaroo", "Cat"]
max_value = max(strings)
print("Maximum value:", max_value, "At index:", strings.index(max_value))
Output:
Maximum value: Kiwi At index: 1
Finding the maximum value in a nested list in Python
A more complex example is finding the maximum value of a nested list in Python.
First, we declare the random values in a nested list.
nst = [[1001, 0.0009], [1005, 0.07682], [1201, 0.57894], [1677, 0.0977]]
From this nested list, we want to get the maximum value of the second element in the nested list. We also want to print out the first element and use it as an index. The way nested lists are handled is more like a key-value pair in a list.
To find the maximum value of this complex structure, we can use the function max()
and its optional argument key
, which remembers the element in the nested list to compare.
nst = [[1001, 0.0009], [1005, 0.07682], [1201, 0.57894], [1677, 0.0977]]
idx, max_value = max(nst, key=lambda item: item[1])
print("Maximum value:", max_value, "At index:", idx)
key
The parameter accepts a lambda function which will allow the function max()
to return a key-value pair.
Output:
Maximum value: 0.57894 At index: 1201
In summary, this tutorial has covered different ways to find the maximum value in different types of lists in Python. Apart max()
from , there are also some built-in functions like min()
, , all()
and any()
, which can operate on iterable objects in Python.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a list to a set in Python
Publish Date:2025/05/08 Views:145 Category:Python
-
This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values in sequence. Python list stores comma separated values between
Remove the first element from a list in Python
Publish Date:2025/05/08 Views:173 Category:Python
-
This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which
Convert a list to lowercase in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin
Remove all occurrences of an element from a Python list
Publish Date:2025/05/08 Views:69 Category:Python
-
In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element
Convert hex to bytes in Python
Publish Date:2025/05/08 Views:102 Category:Python
-
Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for
b in front of string in Python
Publish Date:2025/05/08 Views:54 Category:Python
-
This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v
How to Convert Integer to Binary in Python
Publish Date:2025/05/08 Views:130 Category:Python
-
This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,
How to convert an integer to bytes
Publish Date:2025/05/08 Views:77 Category:Python
-
Converting an integer int to a byte bytes is the inverse of bytes converting a byte to an integer . Most of the to methods int described in this article are the inverse of the to methods. int bytes bytes int Generic method for converting in
How to convert bytes to int in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho