argmin

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

Find index of the minimum.

Parameters:
iterableiterable

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.

keycallable, optional

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

defaultint, optional

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

Returns:
argminint

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