>>> values = [1,2,3,4,1,2] >>> -min((x, -i) for i, x in enumerate(values))[1] 4
There are no changes in the original list, it works for arbitrary iterations and only one pass is required.
This creates iterable tuples with the first value being the source element from the list, and the second being the negative index. When looking for a minimum in this iterable tuple, values ββand then indexes will be compared first, so you get a tuple (min_value, lower_negative_index). By extracting the second element from this tuple and denying it again, you get the highest minimum value index.
Here is an alternate version that is very similar, but uses the key function for min() :
>>> min(range(len(values)), key=lambda i: (values[i], -i)) 4
Please note that this version will only work for sequences (lists, tuples, strings, etc.).
Andrew Clark
source share