5 Ways to Find the list max index in Python

A list is a data structure in python which is used to store items of multiple data types. Because of that, it is considered to be one of the most versatile data structures. We can store items such as string, integer, float, set, list, etc., inside a given list. A list in python is a mutable data type, which means that even after creating a list its elements can be changed. A list is represented by storing its items inside square brackets ‘[ ]’. We can access list elements using indexing. In this article, we shall be looking into how in a python list, we can find the max index.

1. Finding max index using for loop

Finding the maximum index using a for loop is the most basic approach.

my_list = [10,72,54,25,90,40]
max = my_list[0]
index = 0
for i in range(1,len(my_list)):
  if my_list[i] > max:
    max = my_list[i]
    index = i

print(f'Max index is : {index}')

Here, we have taken a list named ‘my_list’, which contains a list of integers. We initially take the first element of the list as the maximum element and store the element into ‘max’. Then we take a variable as ‘index’ and store it with the value 0.

After that, we shall iterate a loop from index 1 to the last element of the list. Inside the loop using an if statement, we shall compare the ith element, i.e., the current element of ‘my_list’ with the ‘max’ variable. If the value of the current element happens to be greater than the value of ‘max’, then we shall assign the value of the current element to ‘max’ and the current index to ‘i’. After completion of the for loop, we shall print the value of ‘index’, which will denote the index of the maximum value from the list.

The output is:

Max index is : 4

An above method is a naive approach. It is for understanding how the maximum element will be found. There are more compact methods, and now we shall be looking into some of them.

2. Using built in methods – max() and index()

We can use python’s inbuilt methods to find the maximum index out of a python list.

The max() method is used to find the maximum value when a sequence of elements is given. It returns that maximum element as the function output. It accepts the sequence as the function argument.

The index() method is used to find the index of a given element from a python list. It accepts the element as an argument and returns the index of that element. In the case of multiple occurrences, it will return the smallest index of that element.

First, we shall use the max() function to find the maximum element from the given list ‘my_list’ and store it in ‘max_item’. Then using the index() function, we shall pass the ‘max_item’ inside the function. Using my_list.index(), we shall return the index of the maximum element and print that.

my_list = [10,72,54,25,90,40]
max_item = max(my_list)
print(f'Max index is : {my_list.index(max_item)}')

The output is:

Max index is : 4

3. Using enumerate() function to find Python list max index

The enumerate() function in python is used to add a counter to an iterable. With the help of enumerate() function, we can find the index of the maximum elements from a list. We shall use list comprehension for storing the index. List comprehension is a way of creating sequences out of already existing sequences.

my_list = [10,72,54,25,90,40]
max_item = max(my_list)
print([index for index, item in enumerate(my_list) if item == max_item])

Using the max() function, we shall store the value of the maximum element into ‘max_item’. Then, we shall enumerate over my_list and check for which list item the value equals max_item. The index for that element shall be printed as a list item.

The output is:

[4]

4. Finding max index for multiple occurrences of elements

If there are multiple occurrences of the maximum element for a list, then we will have to apply a different logic for the same. We will make use of list comprehension to store multiple indexes inside a list.

my_list = [10,72,90,90,54,25,90,40]
max_item = max(my_list)
index_list = [index for index in range(len(my_list)) if my_list[index] == max_item]
print(index_list)

First, using the max() function, we shall find the maximum element from the list. Then, using list comprehension, we shall iterate over the list ‘my_list’, and whenever the item value equals the ‘max_item’, we shall save that index into ‘my_list’. Then, we shall print the ‘index_list’.

The output is:

[2, 3, 6]

5. Maximum index from a numpy array

To find the maximum item index using the numpy library. First, we shall import the numpy library. Then, using the array() function, we shall pass the list my_list as an argument inside the numpy array. This shall convert the given list into a numpy array and store it into ‘n’. Then, using the argmax() function, we shall print the index of the maximum item from the numpy array.

import numpy as np
my_list = [10,72,54,25,90,40]
n = np.array(my_list)
print(f'Max index is : {np.argmax(n)}')

The output is:

Max index is : 4

That wraps up Python List Max Index. If you have any doubts or any thoughts to share, leave them in the comments below.

Until next time, Keep Learning!

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
joao
joao
3 years ago

How the a list with 5 indexes gives you 4?

Pratik Kinage
Admin
3 years ago
Reply to  joao

The list is [10,72,54,25,90,40], so max() will return 90 for this list. And list.index(90) will get you 4.
This will be the index of the maximum value in the list.

Regards,
Pratik