argmin

iteration_utilities.argmin(iterable, /, key, default)

Find index of the minimum.

Parameters:

iterable : iterable

The iterable for which to calculate the index of the minimum.

Note

Instead of one iterable it is also possible to pass the values (at least 2) as positional arguments.

key : callable, optional

If not given then compare the values, otherwise compare key(item).

default : int, optional

If given an empty iterable will return default instead of raising a ValueError.

Returns:

argmin : int

The index of the minimum or default if the iterable was empty.

Examples

This is equivalent (but faster) than min(enumerate(iterable), key=operator.itemgetter(1))[0]:

>>> from iteration_utilities import argmin
>>> argmin(3,2,1,2,3)
2

It allows a key function:

>>> argmin([3, -3, 0], key=abs)
2

And a default:

>>> argmin([], default=10)
10