Select Page

How to Find the Index of the Min Value in a List in Python

by | Programming, Python, Tips

The minimum value in a list is the element with the highest value. You can find the minimum value index in a list by passing the list to the built-in min() function and then using list.index(element) where the element is the minimum value. The list.index() method will return the index of the first occurrence of the minimum value.


Example: List of Numbers

Let’s look at an example of finding the index of the minimum value in a list of numbers.

# Define list

my_list = [2, 5, 10, 99, 4, 1]

# Get min value of the list

min_val = min(my_list)

# Get index of min value

min_idx = my_list.index(min_val)

# Print result

print(f'Min value in {my_list} is {min_val}, with the index {min_idx}')

Let’s run the code to get the result:

Min value in [2, 5, 10, 99, 4, 1] is 1, with the index 5

Note that the index of a list starts from 0.

Example: List of Characters

We can also use the minimum value for characters, for example,

# Define string

my_str = 'twentynine'

# Convert string to list of characters

my_list = list(my_str)

# Get minimum ASCII value

min_val = min(my_list)

# Get the index for the minimum value

min_idx = my_list.index(min_val)

# Print the result

print(f'Min value in {my_list} is {min_val}, with the index {min_idx}')

In the case of characters, the min() function will find the character with the minimum ASCII value. Let’s run the code to get the result:

Min value in ['t', 'w', 'e', 'n', 't', 'y', 'n', 'i', 'n', 'e'] is e, with the index 2

Summary

Congratulations on reading to the end of this tutorial!

For further reading on getting the index of a value from a list, go to the article:

How to Find the Index of the Max Value in a List in Python

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!