How to get the index of the maximum (max) element of the list in Python? If you want to return the maximum element index position from the given list, you can use the max()
to get the maximum value and use this value with index()
method to get the index position of the maximum element. Apart from this method, we will discuss some other methods that will return the maximum element index position.
Methods to get the index of the max element of list in Python:
- Using
max()
method - Using
for
loop - Using
pandas.Series.idxmax()
- Using
numpy.array.argmax()
1. Quick Examples of Finding Index position of Max Element
If you are in a hurry, below are some quick examples of how to get the maximum element index position.
# Quick examples get index of max of list
# Example 1: Get the index of the maximum element
marks = [12,31,40,78,90,32]
max_index = marks.index(max(marks))
# Example 2: Using for loop
marks = [82,31,40,78,90,32,120]
maximum_val= marks[0]
for i in range(1, len(marks)):
if (marks[i] > maximum_val):
maximum_val = marks[i]
max_index = marks.index(maximum_val)
# Example 3: Using for loop
languages = ["punjabi","tamil","malayalam"]
maximum_element= languages[0]
for i in range(1, len(languages)):
if (languages[i] > maximum_element):
maximum_element = languages[i]
max_index = languages.index(maximum_element)
# Example 4: Using pandas.Series.idxmax()
import pandas as pd
marks = [31,40,78,90,32,120]
max_index = pd.Series(marks).idxmax()
# Example 5: Using numpy.array.argmax()
import numpy as np
marks = [91,40,78,90,32,120]
max_index = np.array(marks).argmax()
2. Python Get Index of max() of List
We can use the Python max()
function to get the maximum element and use the list.index() method to get the index position of the maximum element by passing the maximum element to the index() method.
2.1 Syntax
Let’s look at the syntax of how to use max() with index().
# Syntax
mylist1.index(max(mylist1))
Here, mylist1
is the input list.
2.2 Get Index of Max of List Example
Let’s create a list of integers and return the index position of the maximum element. Here, the max(marks)
returns the maximum value of the Python list, and the index(max(marks))
returns the index position of the maximum value from the list.
In the below example, max(<code>marks
) returns the maximum element in the list, and index()
is then used to find its index. Keep in mind that if there are multiple occurrences of the maximum element, this method will return the index of the first occurrence.
# Consider the list of integers
marks = [12,31,40,78,90,32]
# Get the index of the maximum element
max_index = marks.index(max(marks))
print("Index of the maximum element:", max_index)
Yields below output.
Out of 6 integers, the maximum element is 90 and its index position is 4.
3. Using For Loop & index() to Get Max Index
Here, we will iterate all elements in the list and compare whether the element is maximum to the current iterating value, If it is maximum, we will store this maximum value in a variable, and finally using the index()
method, we will return its index position in the list.
3.1 For loop Examples
Example 1: Let’s create a list of integers and return the index position of the maximum element.
# Consider the list of integers
marks = [82,31,40,78,90,32,120]
# Using for loop
maximum_val= marks[0]
for i in range(1, len(marks)):
if (marks[i] > maximum_val):
maximum_val = marks[i]
max_index = marks.index(maximum_val)
print("Maximum Value:",maximum_val)
print("Maximum Index position:",max_index)
Yields below output.
Out of 7 integers, the maximum element is 120 and its index position is 6.
Example 2: Let’s create a list of 3 strings and return the index position of the maximum element.
# Consider the list of strings
languages = ["punjabi","tamil","malayalam"]
maximum_element= languages[0]
# Using for loop
for i in range(1, len(languages)):
if (languages[i] > maximum_element):
maximum_element = languages[i]
max_index = languages.index(maximum_element)
print("Maximum Index position:",max_index)
# Output:
# Maximum Index position: 1
Among 3 languages, “tamil
” is maximum (ASCII value of t is greater than the other two elements), So its index position is returned, i.e 1
4. Using Series.idxmax() to Get Max of Index
The pandas is a module that is used for data analysis available in Python. In this module, a Series is a Data structure that will hold elements in a linear fashion. It will accept idxmax()
method which will return the index position of the maximum element present in the Series.
So we will convert our list to a Series by passing our list to the Series and apply idxmax()
method to return the index position of maximum value.
4.1 Syntax
Let’s look at the syntax of how to use pandas.Series.idxmax()
# Syntax
pd.Series(mylist1).idxmax()
Here, mylist1 is the input list.
4.2 Example
Let’s create a list of integers and return the index position of the maximum element using idxmax()
.
import pandas as pd
# Consider the list of integers
marks = [31,40,78,90,32,120]
# Using pandas.Series.idxmax()
max_index = pd.Series(marks).idxmax()
print("Maximum Index position:",max_index)
# Output:
# MMaximum Index position: 5
Out of 6 integers, the maximum element is 120 and its index position is 5.
5. Using numpy.array().argmax()
The NumPy in Python is a module that will perform mathematical operations on arrays. Here, argmax()
is a method supported by numpy that will return the index of the maximum element in the numpy array. To use NumPy, you need to install it first and import it.
We can utilize this method by converting our list to the numpy array.
5.1 Syntax
Let’s look at the syntax of how to use numpy.array().argmax()
# Syntax
# Here, mylist1 is the input list.
np.array(mylist1).argmax()
5.2 Example
Let’s create a list of integers and return the index position of the maximum element using numpy.argmax()
. Since NumPy works on arrays, first you need to convert the list to the array by using np.array()
function.
import numpy as np
# Consider the list of integers
marks = [91,40,78,90,32,120]
# Using numpy.array.argmax()
max_index = np.array(marks).argmax()
print("Maximum Index position:",max_index)
# Output:
# Maximum marks Index position: 5
Out of 6 integers, the maximum element is 120 and its index position is 5.
Frequently Asked Questions On Get Index of Max of List in Python
To find the index of the maximum element in a Python list, you can use the index
method along with the max
function. For example, max(my_list)
returns the maximum element in the list, and index()
is then used to find its index.
The index
method will return the index of the first occurrence of the maximum element. If you want the index of the last occurrence, you can use the [::-1]
slice to reverse the list and then find the index.
To handle an empty list and avoid errors when trying to find the index of the maximum element, you should check whether the list is empty before attempting to use the index
method.
You can use a loop to find the index of the maximum element in a Python list. Here’s an example using a for
loop and the enumerate
function.
Conclusion
In this article, you have learned how to return the maximum (max) value index position from the Python list using 4 different approaches. First, we discussed using the for
loop & index()
and then directly using max()
& index()
. It can be possible to use idxmax()
by converting our list to the pandas Series and argmax()
by converting our list to the numpy array.