# Getting the index of the returned max or min item using max()/min() on a list

## Getting the index of the returned max or min item using max()/min() on a list

### Question

I'm using Python's `max`

and `min`

functions on lists for a minimax algorithm, and I need the index of the value returned by `max()`

or `min()`

. In other words, I need to know which move produced the max (at a first player's turn) or min (second player) value.

```
for i in range(9):
newBoard = currentBoard.newBoardWithMove([i / 3, i % 3], player)
if newBoard:
temp = minMax(newBoard, depth + 1, not isMinLevel)
values.append(temp)
if isMinLevel:
return min(values)
else:
return max(values)
```

I need to be able to return the actual index of the min or max value, not just the value.

### Accepted Answer

if isMinLevel: return values.index(min(values)) else: return values.index(max(values))

### Popular Answer

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
`numpy`

array

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

Read more... Read less...

You can find the min/max index and value at the same time if you enumerate the items in the list, but perform min/max on the original values of the list. Like so:

```
import operator
min_index, min_value = min(enumerate(values), key=operator.itemgetter(1))
max_index, max_value = max(enumerate(values), key=operator.itemgetter(1))
```

This way the list will only be traversed once for min (or max).

If you want to find the index of max within a list of numbers (which seems your case), then I suggest you use numpy:

```
import numpy as np
ind = np.argmax(mylist)
```

Possibly a simpler solution would be to turn the array of values into an array of value,index-pairs, and take the max/min of that. This would give the largest/smallest index that has the max/min (i.e. pairs are compared by first comparing the first element, and then comparing the second element if the first ones are the same). Note that it's not necessary to actually create the array, because min/max allow generators as input.

```
values = [3,4,5]
(m,i) = max((v,i) for i,v in enumerate(values))
print (m,i) #(5, 2)
```

```
list=[1.1412, 4.3453, 5.8709, 0.1314]
list.index(min(list))
```

Will give you first index of minimum.

I think the best thing to do is convert the list to a `numpy array`

and use this function :

```
a = np.array(list)
idx = np.argmax(a)
```