Top Commentators

  • Two guys and a Beer - Episode 85
  • Jam Session 2
  • Two guys and a Beer - Episode 84

Python: Sort list of tuples by second item

Straight from the PythonInfo Wiki:

>>> import operator
>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
>>> map(operator.itemgetter(0), L)
['c', 'd', 'a', 'b']
>>> map(operator.itemgetter(1), L)
[2, 1, 4, 3]
>>> sorted(L, key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Posterous
  • Reddit
  • Twitter
  • HackerNews
  • FriendFeed
  • Hyves
  • RSS

Related:

  1. Django template filter: Show list of objects as table with fixed number of columns
  2. JIRA issues reporting with Python

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>