Skip to content

Instantly share code, notes, and snippets.

@samstewart
Created November 13, 2011 22:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samstewart/1362847 to your computer and use it in GitHub Desktop.
Save samstewart/1362847 to your computer and use it in GitHub Desktop.
Find the index of a maximum or minimum element of a python list
#Gets the index of maximum element in a list. If a conflict occurs, the index of the last largest is returned
def maxl(l): return l.index(reduce(lambda x,y: max(x,y), l))
#Gets the index of minimum element in a list. If a conflict occurs, the index of the last smallest is returned
def minl(l): return l.index(reduce(lambda x,y: min(x,y), l))
#same as above but without the reduce coolness
def maxl(l): return l.index(max(l))
def minl(l): return l.index(min(l))
@samstewart
Copy link
Author

Love functional programming!

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