Skip to content

Instantly share code, notes, and snippets.

@jaumebonet
Last active August 29, 2015 14:19
Show Gist options
  • Save jaumebonet/f59529993748c0e6b6c0 to your computer and use it in GitHub Desktop.
Save jaumebonet/f59529993748c0e6b6c0 to your computer and use it in GitHub Desktop.
Sort list of dictionaries by value of a given key
# from: http://stackoverflow.com/a/73050/2806632
from operator import itemgetter
l = [] # List of dictionaries
k = '' # Key of interest to sort by
# [..] fill list 'l' with dictionaries containing a 'k' key
newlist = sorted(l, key=itemgetter(k), reverse=True) # reverse boolean for ascending/descending sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment