Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case.
Avoid the solution with itemgetter() presented in the other answers, and use instead
index_min = min(range(len(values)), key=values.__getitem__)
because it doesn't require to import operator nor to use enumerate, and it is always faster(benchmark below) than a solution using itemgetter().
If you are dealing with numpy arrays or can afford numpy as a dependency, consider also using
import numpy as np
index_min = np.argmin(values)
This will be faster than the first solution even if you apply it to a pure Python list if:
- it is larger than a few elements (about 2**4 elements on my machine)
- you can afford the memory copy from a pure list to a
numpyarray
as this benchmark points out:

I have run the benchmark on my machine with python 2.7 for the two solutions above (blue: pure python, first solution) (red, numpy solution) and for the standard solution based on itemgetter() (black, reference solution).
The same benchmark with python 3.5 showed that the methods compare exactly the same of the python 2.7 case presented above
Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case.
Avoid the solution with itemgetter() presented in the other answers, and use instead
index_min = min(range(len(values)), key=values.__getitem__)
because it doesn't require to import operator nor to use enumerate, and it is always faster(benchmark below) than a solution using itemgetter().
If you are dealing with numpy arrays or can afford numpy as a dependency, consider also using
import numpy as np
index_min = np.argmin(values)
This will be faster than the first solution even if you apply it to a pure Python list if:
- it is larger than a few elements (about 2**4 elements on my machine)
- you can afford the memory copy from a pure list to a
numpyarray
as this benchmark points out:

I have run the benchmark on my machine with python 2.7 for the two solutions above (blue: pure python, first solution) (red, numpy solution) and for the standard solution based on itemgetter() (black, reference solution).
The same benchmark with python 3.5 showed that the methods compare exactly the same of the python 2.7 case presented above
Find the minimum value with min() then find that value's index with .index():
values.index(min(values))
Or the maximum:
values.index(max(values))
If your list contains repeats of the minimum or maximum value this will return the index of the first one.
finding max in list
[python] index of absolute maximum value in a list
A quicker way to get min and max date from pandas series
IndexError: list index out of range using dict...?
Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:
You appear to be using concatenation and the str function for building strings
Instead of doing something like
result = "Hello " + name + ". You are " + str(age) + " years old"
You should use string formatting and do
result = "Hello {}. You are {} years old".format(name, age)See the python tutorial for more information.
More on reddit.comVideos
#I have 2 lists. First, i need to find the max value in list one. the value i want #from list 2 should be the one found in the same index as the max in list one. #example: since 135 is larger, i want to return 15 #because 135 is index 1, and 15 is index 1. LIST_1 = [101,135] LIST_2=[20,15] #i would really appreciate any help, any hints! :D