Skip to content

Instantly share code, notes, and snippets.

@kirang89
Created March 26, 2013 09:27
Show Gist options
  • Save kirang89/5244130 to your computer and use it in GitHub Desktop.
Save kirang89/5244130 to your computer and use it in GitHub Desktop.
Sorting a list of tuples based on either value in the tuple
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