Python: How to 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.

Additional Resources

How to Zip Two Lists in Python
How to Convert a List to a DataFrame in Python
How to Plot Histogram from List of Data in Python

Leave a Reply

Your email address will not be published.