Skip to content

Instantly share code, notes, and snippets.

@satishgoda
Created September 18, 2014 13:45
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 satishgoda/150d8f58d2dbfb5111cd to your computer and use it in GitHub Desktop.
Save satishgoda/150d8f58d2dbfb5111cd to your computer and use it in GitHub Desktop.
operator.methodcaller
import operator
last = operator.itemgetter(-1)
l1 = []
l2 = [1,2,3]
l3 = [-100, 10, 400]
def gaurdedLast(l):
return (last(l) if l else None)
print map(gaurdedLast, (l1, l2, l3))
# [None, 3, 400]
print {i: gaurdedLast(eval(i)) for i in ('l1', 'l2', 'l3')}
#{'l1': None, 'l2': 3, 'l3': 400}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment