Skip to content

Instantly share code, notes, and snippets.

@eyaltrabelsi
Created July 5, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyaltrabelsi/a417d85d039e1df86715bef3ff808821 to your computer and use it in GitHub Desktop.
Save eyaltrabelsi/a417d85d039e1df86715bef3ff808821 to your computer and use it in GitHub Desktop.
Python trick for getting max/min values from iterables
# Getting maximum from iterable
>>> a = [1, 2, -3]
>>> max(a)
2
# Getting maximum from iterable
>>> min(a)
1
# Bot min/max has key value to allow to get maximum by appliing function
>>> max(a,key=abs)
3
@vcyriac
Copy link

vcyriac commented Nov 19, 2019

Shouldn't getting minimum of a
min(a) output -3

while min(a, key=abs) will output 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment