Last active
August 29, 2015 14:19
-
-
Save jaumebonet/f59529993748c0e6b6c0 to your computer and use it in GitHub Desktop.
Sort list of dictionaries by value of a given key
This file contains hidden or 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: 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