Created
March 26, 2013 09:27
-
-
Save kirang89/5244130 to your computer and use it in GitHub Desktop.
Sorting a list of tuples based on either value in the tuple
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
from operator import itemgetter | |
a = [('Ant', 1), ('Elephant', 2), ('Cow', 3), ('Deer', 4)] | |
print "List sorted according to animal name: ", sorted(a, key=itemgetter(0,1)) | |
print "List sorted according to number: ", sorted(a, key=itemgetter(1,0)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment