Open In App

Python – Find index of maximum item in list

Last Updated : 27 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of N integers, the task is to write a Python program to find the index of the maximum element in the Python list.

Example:

Input: [  2, 4, 6, 1, 8, 5, 3 ]
Output:  Index of the max element in a list is 4
Explanation: The max element is 8 and its position is 4.

Input: [  10, 1, 5, 3, 8, 9, 7 ]
Output:  Index of the max element in a list is 0
Explanation: The max element is 10 and its position is 0.

Get the index of the max value without using in-built functions

We are given list of integers. Our task is to find the index of the maximum element.

Python3




#list of numbers
list1 = 2, 4, 6, 1, 8, 5, 3 ]
ind = 0    #ind variable to store the index of maximum value in the list
max_element = list1[0]
 
for i in range (1,len(list1)): #iterate over array
  if list1[i] > max_element: #to check max value
    max_element = list1[i]
    ind = i
#print the index of maximum element
print("Index of the maximum element in the list is: ",ind)


Output

Index of the maximum element in the list is:  4

Time Complexity: O(n),
Auxiliary space: O(1)

Get the index of the max value in the list using the max() and index() 

Here we are given a Python list and our task is to find the index of the maximum element so we are finding the maximum element using max() and then finding the index of that element using index() in Python.

Python3




# Defining a list
l = [1, 4, 3, 7, 8, 10, 2]
 
# Calculating the maximum element
# in the list
m = max(l)
 
# Finding the index of the maximum
# element
print("Index of the max element in a list is", l.index(m))


Output

Index of the max element in a list is 5

Time Complexity: O(n),
Auxiliary space: O(1)

Get the index of the max value in the list using the loop 

Here we are given a list and our task is to find the index of the maximum element so we are finding the maximum element using max() and then finding the index of that element using a while loop and iterating over the list.

Python3




# Defining a list
l = [1, 4, 3, 7, 8, 10, 2]
 
# Calculating the maximum element
# in the list
m = max(l)
 
# Finding the index of the maximum
# element
i = 0
while(i < len(l)):
    if l[i] == m:
        print("Index of the max element in a list is", i)
        break
    i += 1


Output

Index of the max element in a list is 5

Time Complexity: O(n),
Auxiliary space: O(1)

Find the index of the max value in a list  using the enumerate function

Here we are given a list and our task is to find the index of the maximum element so we are iterating over the list using the enumerate() function as the index, pair. If we have the same element at two positions we will return both indexes.

Python3




# Defining the list
l = [12, 22, 4, 3, 7, 8, 10, 22]
 
# Finding maximum element
m = max(l)
 
# iterating over the list like index,
# item pair
for i, j in enumerate(l):
 
    if j == m:
        print("Index of the max element in a list is", i)


Output

Index of the max element in a list is 1
Index of the max element in a list is 7

Time Complexity: O(n),
Auxiliary space: O(1)

Find the index of the max value in a list using Pandas

Here we are given a list and our task is to find the index of the maximum element so we are finding its index using idxmax() in the Pandas module.

Python3




# importing the required library
import pandas as p
 
# Defining the list
l = [12, 4, 3, 7, 8, 10, 22]
 
# Printing the index of the maximum element
print("Index of the max element in a list is", p.Series(l).idxmax())


Output:

Index of the max element in a list is 6

Time Complexity: O(n),
Auxiliary space: O(1)

Find the index of the max value in a list  using Numpy 

Here we are given a list and our task is to find the index of the maximum element so we are finding its index using argmax() in the Numpy module.

Python3




# importing the required library
import numpy
 
# Defining the list
l = [12, 4, 3, 7, 8, 10, 22]
 
# argmax returns the max element's index
idx = numpy.argmax(l)
 
print("Index of the max element in a list is", idx)


Output:

Index of the max element in a list is 6

Time Complexity: O(n),
Auxiliary space: O(1)

Sort the list in descending order

One new approach that is not discussed in the article is to use the built-in sorted() function to sort the list in descending order, and then access the first element of the sorted list to get the index of the maximum item in the list.

Python3




# Defining a list
l = [1, 4, 3, 7, 8, 10, 2]
  
# Sorting the list in descending order
sorted_list = sorted(l, reverse=True)
 
# Accessing the first element of the sorted list
max_element = sorted_list[0]
 
# Finding the index of the maximum element
print("Index of the max element in a list is", l.index(max_element))


Output

Index of the max element in a list is 5

Time Complexity: O(nlogn),
Auxiliary space: O(1)

Find index of maximum item in list Using Recursion

We are using recursive method to find the index of maximum element in the list.

Python3




# Recursive function to find the index of maximum of element
def FindIndex(itr,ind,list1):
  if itr == len(list1): #base Condition
    print("Index of the maximum element in the list is : ",ind)
    return
  if list1[itr] > list1[ind]: #max condition
    ind = itr
  FindIndex(itr+1,ind,list1) #Recursive Function call
  return
#Driver code
list1 = [2,4,1,9,0,8]
FindIndex(0,0,list1)
 
#This code is Contributed by Jyothi Pinjala.


Output

Index of the maximum element in the list is :  3

Time Complexity: O(n),
Auxiliary space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads