Index of Maximum Value in a Python List

Finding the index of the maximum value in a list or an array is one of the most common coding interview questions you can get in any coding interview. Here you have to find the index of the maximum value instead of the maximum value itself. So if you want to learn how to find the index of the maximum value in a Python list, then this article is for you. In this article, I’ll walk you through how to find the index of the maximum value in a Python list.

Index of the Maximum Value in a Python List

Python has a built-in function to find the maximum value in a list. You can also define your Python function for the same task. But the question here is to find the index of the maximum value instead of the value itself. So here is how you can define a Python function to find the index of the maximum value in a Python list:

def maximum(x):
maximum_index = 0
current_index = 1
while current_index < len(x):
if x[current_index] > x[maximum_index]:
maximum_index = current_index
current_index = current_index + 1
return maximum_index
a = [23, 76, 45, 20, 70, 65, 15, 54]
print(maximum(a))
view raw maximum.py hosted with ❤ by GitHub
Output: 1

Also, Read – Index of Minimum Value in a Python List.

The above function implements an algorithm to find the index of the maximum value. It assumes that the list is not empty and that the items in the list are in random order. It starts by treating the first element in the list as the maximum element, then it searches for the largest element on the right, if it finds an element larger than the first, it resets the position of the maximum element. When it reaches the last item in the list by following the same process, it returns the index of the maximum value.

Summary

So this is how you can find the index of the maximum item in a Python list. It is one of the popular coding interview questions that you may get in any coding interview. I hope you liked this article on how to find the index of the maximum value using the Python programming language. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1170

Leave a Reply