Skip to content

Instantly share code, notes, and snippets.

@lxneng
Created February 26, 2010 02:47
Show Gist options
  • Save lxneng/315334 to your computer and use it in GitHub Desktop.
Save lxneng/315334 to your computer and use it in GitHub Desktop.
Python sorting - A list of objects
In [1]: import operator
In [2]: L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
In [3]: sorted(L, key=operator.itemgetter(0))
Out[3]: [('a', 4), ('b', 3), ('c', 2), ('d', 1)]
In [4]: sorted(L, key=operator.itemgetter(1))
Out[4]: [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment