• 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 Minimum in List Using Python

Find Index of Minimum in List Using Python

February 24, 2022 Leave a Comment

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

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]

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

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

min_of_list = min(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers ) if j == min_of_list]

print(index)
  
#Output:
[7]

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 minimum of a list of numbers.

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

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

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.

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

Another way we can get the position of the minimum 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 minimum of a list by returning the index where any value is equal to the minimum of the list.

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

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

min_of_list = min(list_of_numbers)
index = [i for i, j in enumerate(list_of_numbers) if j == min_of_list]

print(index)
  
#Output:
[7]

Find Index of Maximum in List with Python

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

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.

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

Other Articles You'll Also Like:

  • 1.  Remove Empty Strings from List in Python
  • 2.  Using Python to Generate Random String of Specific Length
  • 3.  Break Out of Function in Python with return Statement
  • 4.  Python Get Operating System Information with os and platform Modules
  • 5.  Get Last Character in String in Python
  • 6.  Ceiling Division in Python
  • 7.  How to Read XLSX File from Remote Server Using Paramiko FTP and Pandas
  • 8.  Python Destroy Object – How to Delete Objects with del Keyword
  • 9.  Perfect Numbers in Python
  • 10.  Python Factorial Recursion – Using Recursive Function to Find Factorials

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

x