How can I find the index of the maximum value in a list using Python?

How can I find the index of the maximum value in a list using Python?

The process of finding the index of the maximum value in a list using Python involves using built-in functions or methods such as “max()” and “index()”. The “max()” function is used to find the maximum value in a given list, while the “index()” method is used to retrieve the index of a specific element in a list. By combining these two functions, one can easily find the index of the maximum value in a list. This approach is efficient and time-saving, making it a popular method among Python programmers.

Python: Find Index of Max Value in List


You can use the following syntax to find the index of the max value of a list in Python:

#find max value in list
max_value = max(list_name)

#find index of max value in list 
max_index = list_name.index(max_value)

The following examples show how to use this syntax in practice.

Example 1: Find Index of Max Value in List

The following code shows how to find the max value in a list along with the index of the max value:

#define list of numbers
x = [9, 3, 22, 7, 15, 16, 8, 8, 5, 2]

#find max value in list
max_value = max(x)

#find index of max value in list
max_index = x.index(max_value)

#display max value
print(max_value)

22

#display index of max value
print(max_index)

2

The maximum value in the list is 22 and we can see that it’s located at index value 2 in the list.

Note: Index values start at 0 in Python.

Example 2: Find Index of Max Value in List with Ties

The following code shows how to find the max value in a list along with the index of the max value when there are multiple max values.

#define list of numbers with multiple max values
x = [9, 3, 22, 7, 15, 16, 8, 8, 5, 22]

#find max value in list
max_value = max(x)

#find indices of max values in list
indices = [index for index, val in enumerate(x) if val == max_value]

#display max value
print(max_value)

22

#display indices of max value
print(indices)

[2, 9]

The maximum value in the list is 22 and we can see that it occurs at index values 2 and 9 in the list.

Cite this article

stats writer (2024). How can I find the index of the maximum value in a list using Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-list-using-python/

stats writer. "How can I find the index of the maximum value in a list using Python?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-list-using-python/.

stats writer. "How can I find the index of the maximum value in a list using Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-list-using-python/.

stats writer (2024) 'How can I find the index of the maximum value in a list using Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-list-using-python/.

[1] stats writer, "How can I find the index of the maximum value in a list using Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I find the index of the maximum value in a list using Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top