Skip to content

Instantly share code, notes, and snippets.

@seahrh
Created August 5, 2020 23:28
Show Gist options
  • Save seahrh/5f6576959bebe6314c5b442ce2b33647 to your computer and use it in GitHub Desktop.
Save seahrh/5f6576959bebe6314c5b442ce2b33647 to your computer and use it in GitHub Desktop.
argmin, argmax in Python3
from typing import Iterable, TypeVar
T = TypeVar("T")
def argmin(elements: Iterable[T]) -> int:
"""Returns first index of smallest element."""
return min(enumerate(elements), key=lambda x: x[1])[0]
def argmax(elements: Iterable[T]) -> int:
"""Returns first index of largest element."""
return max(enumerate(elements), key=lambda x: x[1])[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment