Created
September 18, 2014 13:45
-
-
Save satishgoda/150d8f58d2dbfb5111cd to your computer and use it in GitHub Desktop.
operator.methodcaller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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