Abstracting the process of finding the index of the maximum in Python
I just want to find the index of the "best" item.


This can be written very shortly and readably using enumerate, first class functions and list indexing.

def index_of_max_value(items):
    return max(enumerate(items), key = lambda x: x[1])[0]


Explanation:


I create a list that contains the numbers and their indexes, the index is first, the number second.




&gt;&gt;&gt; list(enumerate([4,7,4,8]))
[(0, 4), (1, 7), (2, 4), (3, 8)]



I ask the item that has the biggest value when a function I decide is applied to it, I do so by passing the function as the key argument, when functions are passed around like this they are first class citizens




&gt;&gt;&gt; max(["rg","gu","sd"], key = lambda s: sum(map(ord,s)))
'gu'


For example in the above I asked which of the strings had the most ASCII value.

key = lambda x: x[1]


Means that I want the pair (index,number) that has the biggest 'second' item (remember zero indexing).


[n] gives you the n-th item of a list, I ask the '0-th' (first) item that is the index.


I hope that you like my alternative shorter solution, if you have any doubts, fell free to ask.