Custom Search

Wednesday, February 27, 2013

How To Python Sorting List of Dictionaries

ldict = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])

import operator
newlist = sorted(list_to_be_sorted, key=operator.itemgetter('name'))

import operator
ldict.sort(key=operator.itemgetter('name'))

No comments:

Post a Comment