Python – Find index of maximum item in list
Last Updated :
27 Jul, 2023
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
list1 = [ 2 , 4 , 6 , 1 , 8 , 5 , 3 ]
ind = 0
max_element = list1[ 0 ]
for i in range ( 1 , len (list1)):
if list1[i] > max_element:
max_element = list1[i]
ind = i
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
l = [ 1 , 4 , 3 , 7 , 8 , 10 , 2 ]
m = max (l)
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
l = [ 1 , 4 , 3 , 7 , 8 , 10 , 2 ]
m = max (l)
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
l = [ 12 , 22 , 4 , 3 , 7 , 8 , 10 , 22 ]
m = max (l)
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
import pandas as p
l = [ 12 , 4 , 3 , 7 , 8 , 10 , 22 ]
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
import numpy
l = [ 12 , 4 , 3 , 7 , 8 , 10 , 22 ]
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
l = [ 1 , 4 , 3 , 7 , 8 , 10 , 2 ]
sorted_list = sorted (l, reverse = True )
max_element = sorted_list[ 0 ]
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
def FindIndex(itr,ind,list1):
if itr = = len (list1):
print ( "Index of the maximum element in the list is : " ,ind)
return
if list1[itr] > list1[ind]:
ind = itr
FindIndex(itr + 1 ,ind,list1)
return
list1 = [ 2 , 4 , 1 , 9 , 0 , 8 ]
FindIndex( 0 , 0 ,list1)
|
Output
Index of the maximum element in the list is : 3
Time Complexity: O(n),
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...