• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Find Index of Maximum in List Using Python

Find Index of Maximum in List Using Python

February 24, 2022 Leave a Comment

In Python, we can easily find the index of the maximum in a list of numbers. The easiest way to get the index of the maximum in a list with a loop.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def maxIndex(lst):
    index = [0]
    max = lst[0]
    for i in range(1,len(lst)):
        if lst[i] > max:
            max = lst[i]
            index = [i]
        elif lst[i] == max:
            index.append(i)
    return index

print(maxIndex(list_of_numbers))

#Output:
[2]

You can also use enumerate() and list comprehension to get the index of the maximum of a list in Python.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]

max_of_list = max(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers) if j == max_of_list]

print(index)
  
#Output:
[2]

There are many powerful built-in functions in the Python language which allow us to perform basic or complex tasks easily.

One such task is to find the maximum of a list of numbers.

However, what if you want to find the index of the maximum?

We can easily find the index of the maximum value in a list using a loop in Python.

To find the index of the maximum of a list, we just need to keep track of the maximum and the index of the maximum, and loop over the list of numbers.

Below is an example of a Python function which gets the index of the maximum of a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def maxIndex(lst):
    index = [0]
    max = lst[0]
    for i in range(1,len(lst)):
        if lst[i] > max:
            max = lst[i]
            index = [i]
        elif lst[i] == max:
            index.append(i)
    return index

print(maxIndex(list_of_numbers))

#Output:
[2]

If you are using pandas, you can find the index of the maximum of a column or DataFrame with the pandas idxmax() function.

Using enumerate() and List Comprehension to Find Index of Maximum of List in Python

Another way we can get the position of the maximum of a list in Python is to use list comprehension and the enumerate() function.

The enumerate() function takes in a collection of elements and returns the values and indices of each item of the collection.

We can get use enumerate() and list comprehension to get the index of the maximum of a list by returning the index where any value is equal to the maximum of the list.

Below is how to use enumerate() and list comprehension to get the index of the maximum of a list in Python.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]

max_of_list = max(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers) if j == max_of_list]

print(index)
  
#Output:
[2]

Find Index of Maximum in List with Python

We can adjust the function which finds the index of the maximum in a list to find the index of the minimum in a list.

To find the index of the minimum of a list, we just need to keep track of the minimum and the index of the minimum, and loop over the list of numbers.

Below is an example of a Python function which gets the index of the minimum of a list of numbers.

list_of_numbers = [2, 1, 9, 8, 6, 3, 1, 0, 4, 5]
  
def minIndex(lst):
    index = [0]
    min = lst[0]
    for i in range(1,len(lst)):
        if lst[i] < min:
            min = lst[i]
            index = [i]
        elif lst[i] == min:
            index.append(i)
    return index

print(minIndex(list_of_numbers))

#Output:
[7]

If you are using pandas, you can find the index of the minimum of a column or DataFrame with the pandas idxmin() function.

Hopefully this article has been useful for you to understand how to find the index of the maximum of a list in Python.

Other Articles You'll Also Like:

  • 1.  How to Check if String Contains Lowercase Letters in Python
  • 2.  Using Python to Find Second Largest Value in List
  • 3.  How to Multiply All Elements in List Using Python
  • 4.  pandas ewm – Calculate Exponentially Weighted Statistics in DataFrame
  • 5.  How to Write Python Dictionary to File
  • 6.  Find Maximum of Three Numbers in Python
  • 7.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 8.  Remove Element from Set in Python
  • 9.  How to Remove Vowels from a String in Python
  • 10.  Using Python to Calculate Average of List of Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy