Skip to content Skip to sidebar Skip to footer

Sort Dictionaries Based On Timestamp In Python

Here is my list , [{u'lang': u'en', u'createdAt': 1326158349, u'canonicalUrl': u'https://foursquare.com/item/4f0b920de4b0c4aaa8bc78ec', u'text': u'one of the great restaurant in ne

Solution 1:

Use list.sort (inplace sorting) or sorted (returns new sorted list) and pass key function. The return value of the key function is used for comparison:

lst.sort(key=lambda d: d['createdAt'])

Solution 2:

You can use operator.itemgetter as key with either sorted or list.sort:

fromoperator import itemgetter
my_list.sort(key=itemgetter('createdAt'))

Post a Comment for "Sort Dictionaries Based On Timestamp In Python"