argmax¶
- iteration_utilities.argmax(iterable, /, key=None, default=None)¶
Find index of the maximum.
- Parameters:
- iterableiterable
The iterable for which to calculate the index of the maximum.
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)
.- default
int
, optional If not given raise
ValueError
if the iterable is empty otherwise returndefault
- Returns:
- argmax
int
The index of the maximum or default if the iterable was empty.
- argmax
Examples
This is equivalent (but faster) than
max(enumerate(iterable), key=operator.itemgetter(1))[0]
:>>> from iteration_utilities import argmax >>> argmax(3,2,1,2,3) 0
It allows a key function:
>>> argmax([0, -3, 3, 0], key=abs) 1
And a default:
>>> argmax([], default=10) 10