# Find the Index of Maximum Item in a List using Python

In Python, the index of maximum item refers to a specific index in which the item will be greater than all the elements of the list. In Python, we have some built−in functions len(), max(), series(), idxmax(), and index() that can be used to find the index of maximum item in a list.

Let’s take an example of this.

The given string, [61, 12, 4, 5, 89, 7]

So, the maximum present index becomes 4 i.e. 89.

### Syntax

The following syntax is used in the examples-

len()


The len() is a built−in method in Python that returns the object length.

max()


The max() function is an in−built function in Python that finds the element with the maximum value or in an iteration.

Series()


This built−in function is defined by a one−dimensional array that is able to hold the data of any type such as integer, string, float, etc.

idxmax()


This built−in function returns the series of indexes with the maximum value from the list.

index()


The index() is a built−in function in Python that can be used to search the element from the list and return the position of the element.

## Using while loop

The program uses a while loop to iterate through the loop and check the condition with use method named len() which is used to determine the length of the input list.

### Example

In the following example, start the program by storing the input list in the variable marks. Then initialize the two variables− maximum_val and maximum_index to the first value in the list and its index respectively. Then the program initializes the variable i to 1 for loop iteration. Now using the while loop to iterate through the list. Then using an if−statement for each iteration, it checks if the current value in the list is greater than the current maximum value. Then it updates the maximum_val variable to the current value and the maximum_index variable to the current index and the loop variable i is incremented by 1. Finally, we are printing the result with the help of variable maximum_val and maximum_index.

marks = [1086, 1044, 1221, 3669, 1000, 2843]
# initialize maximum value and index
maximum_val = marks
maximum_index = 0
# Initialize loop variable
i = 1
# iterate through the list using while loop
while i < len(marks):
if marks[i] > maximum_val:
maximum_val = marks[i]
maximum_index = i
i += 1
print(f"Maximum Value: {maximum_val}")
print(f"Maximum Index position: {maximum_index}")


### Output

Maximum Value: 3669
Maximum Index position: 3


## Using max() Function

The program uses the built−in function max() to find the maximum item in a list using Python.

### Example

In the following example, begin the program by storing the input list in the variable list1. Then use the built−in function max() that accepts the parameter as list1 to find the maximum value from the list and store it in the variable max_number. Finally, print the output with the help of the variable max_number.

list1 = [3, 2, 8, 5, 10, 6]
max_number = max(list1);
print("The largest number is:", max_number)


### Output

The largest number is: 10


## Using Series() and idxmax() Function

The program uses the Pandas module to provide its built−in function Series() and idxmax() to find the index of maximum items in a list.

### Example

In the following example, we will start the program by importing the module named pandas and taking reference as pds which will help to set the built−in function related to it. Then create the input list and store it in the variable Age. Next, using the built−in functions− Series() and idxmax() it will find the series of lists in an integer form and return the maximum index item from the list.

import pandas as pds
# Consider the list of integers
Age = [39, 126, 40, 50, 77, 87]
# Using pandas.Series().idxmax()
print("Maximum Index position from the list: ",pds.Series(Age).idxmax())


### Output

Maximum Index position from the list:  1


## Using index() Function

The program uses a built−in function index() along with max() to return the highest value index from the given list.

### Example

In the following example, begin the program by storing the input list in the variable Score. Then use the built−in functions index() with variable Score to find the item position and it accepts the parameter as built−in function max() which returns the maximum item from the list.

Score = [41, 35, 59, 78, 99, 100, 33]
print("Maximum index present in the list:", Score.index(max(Score)))


### Output

Maximum index present in the list: 5


## Using simple for loop and if−statement

The program uses a simple for loop to iterate through the list and using an if−statement it will check the maximum index item from the list.

### Example

In the following example, start the program with a function named max_num(). Inside the function, the input list is stored in the variable num. Then use the variable num with index 0 i.e. num in the variable max_value which will help to check the condition in if−statement. Next, using the for loop the variable a iterate through the list i.e. num, and using if−statement it set the condition for a maximum item using the > operator and return the maximum item from the list in the variable max_value. Then it will use the index() with variable num to find the maximum index and store it in the variable max_index. Finally, we are printing the result with the help of a calling function named max_num().

def max_num():
num = [120, 50, 99, 102, 11, 91]
max_value = num
for a in num:
if a > max_value:
max_value = a
max_index = num.index(max_value)
print("The maximum value from the list is:", max_value)
print("The index of the max value is:", max_index)
max_num()


### Output

The maximum value from the list is: 120
The index of the max value is: 0


## Conclusion

We saw the above outputs represent the maximum number along its index position from the list. There are new ways to solve the problem statement such as idxmax() and series() which define the 1D array to hold any data type and return index series with the highest value. This type of program is generally used to solve the problem based on File Record System and Filter the maximum value in database.

Updated on: 14-Aug-2023

2K+ Views 