Get Index of Max of List in Python

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:

  1. Using max() method
  2. Using for loop
  3. Using pandas.Series.idxmax()
  4. Using numpy.array.argmax()

1. Quick Examples of Finding Index position of Max Element

Following are quick examples of how to get the maximum element index position.


# Quick Examples

# Consider the list of integers
marks = [82,31,40,78,90,32,120]
 
# Using index() method
print("Maximum Index position: ",marks.index(max(marks))) 

# Using for loop
maximum_val= marks[0]
for i in range(1, len(marks)): 
    if (marks[i] > maximum_val):
        maximum_val = marks[i]
print("Maximum Index position: ",marks.index(maximum_val))

# Using pandas.Series.idxmax()
import pandas as pd
print("Maximum Index position: ",pd.Series(marks).idxmax()) 

# Using numpy.array.argmax()
import numpy as np
print("Maximum Index position: ",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.


# Consider the list of integers
marks = [12,31,40,78,90,32]
 
# Using index() method
print("Maximum Index position: ",marks.index(max(marks)))  

# Output:
# Maximum Index position:  4

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]

print("Maximum Value:",maximum_val)
print("Maximum Index position: ",marks.index(maximum_val))  

# Output:
# Maximum Value: 120
# Maximum Index position:  6

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]

print("Maximum Index position: ",languages.index(maximum_element))  

# 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()
print("Maximum Index position: ",pd.Series(marks).idxmax())  

# 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() funcion.


import numpy as np
# Consider the list of integers
marks = [91,40,78,90,32,120]
 
# Using numpy.array.argmax()
print("Maximum Index position: ",np.array(marks).argmax())

# Output:
# Maximum marks Index position:  5

Out of 6 integers, the maximum element is 120 and its index position is 5.

6. 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 pandas Series and argmax() by converting our list to the numpy array.

Leave a Reply