Skip to content

Instantly share code, notes, and snippets.

@Ogaday
Last active December 1, 2022 19:20
Show Gist options
  • Select an option

  • Save Ogaday/c26eaa8bbf812b1792f1163417ba0ef6 to your computer and use it in GitHub Desktop.

Select an option

Save Ogaday/c26eaa8bbf812b1792f1163417ba0ef6 to your computer and use it in GitHub Desktop.
Native Argmax
"""
A native implementation of argmax.
https://towardsdatascience.com/there-is-no-argmax-function-for-python-list-cd0659b05e49
Tests::
pytest --doctest-modules -vvv argmax.py
Linting::
mypy argmax
"""
from typing import Sequence
def argmax(iterable: Sequence) -> int:
"""
Return the index of the largest item in an iterable.
Parameters
----------
iterable : Sequence
The object in which to find the largest item.
Examples
--------
>>> argmax([3, 31, 29])
1
"""
return max(range(len(iterable)), key=lambda i: iterable[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment