list - Sort dictionaries based on timestamp in python -
here list ,
[{u'lang': u'en', u'createdat': 1326158349, u'canonicalurl': u'https://foursquare.com/item/4f0b920de4b0c4aaa8bc78ec', u'text': u'one of great restaurant in new haven , excellent food , service', u'user': {u'lastname': u's.', u'gender': u'male', u'id': u'19483764', u'firstname': u'ram', u'photo': {u'prefix': u'https://irs0.4sqi.net/img/user/', u'suffix': u'/blank_boy.png'}}, u'todo': {u'count': 0}, u'id': u'4f0b920de4b0c4aaa8bc78ec', u'logview': true, u'likes': {u'count': 2, u'groups': [{u'count': 2, u'items': [], u'type': u'others'}], u'summary': u'2 likes'}}, {u'lang': u'en', u'createdat': 1305643797, u'canonicalurl': u'https://foursquare.com/item/4dd28b15227159fef24d285a', u'url': u'http://www.goscoville.com', u'text': u'buffet quality alright, , cheap', u'user': {u'lastname': u'e.', u'gender': u'male', u'id': u'2617284', u'firstname': u'perry', u'photo': {u'prefix': u'https://irs1.4sqi.net/img/user/', u'suffix': u'/1owjl2fylhwgjgzs.jpg'}}, u'todo': {u'count': 0}, u'id': u'4dd28b15227159fef24d285a', u'logview': true, u'likes': {u'count': 1, u'groups': [{u'count': 1, u'items': [], u'type': u'others'}], u'summary': u'1 like'}}, {u'lang': u'en', u'createdat': 1352054660, u'canonicalurl': u'https://foursquare.com/item/5096b784e4b079e230cea793', u'text': u"they're not talented meat dishes, come here excellent vegetarian meals, majority of menu.", u'user': {u'lastname': u'p.', u'gender': u'male', u'id': u'5505251', u'firstname': u'alex', u'photo': {u'prefix': u'https://irs0.4sqi.net/img/user/', u'suffix': u'/5505251-kawix0mmdupyis2c.jpg'}}, u'todo': {u'count': 0}, u'id': u'5096b784e4b079e230cea793', u'logview': true, u'likes': {u'count': 0, u'groups': []}}, {u'lang': u'en', u'createdat': 1337213142, u'canonicalurl': u'https://foursquare.com/item/4fb440d6e4b002d950289ae6', u'text': u'stay away long haired creepy waiter! shrimp garlic sauce reportedly delightful.', u'user': {u'lastname': u'g.', u'gender': u'female', u'id': u'28141671', u'firstname': u'molly', u'photo': {u'prefix': u'https://irs2.4sqi.net/img/user/', u'suffix': u'/blank_girl.png'}}, u'todo': {u'count': 0}, u'id': u'4fb440d6e4b002d950289ae6', u'logview': true, u'likes': {u'count': 0, u'groups': []}}, {u'lang': u'en', u'createdat': 1329162806, u'canonicalurl': u'https://foursquare.com/item/4f396a36e4b0653d4cc83fad', u'text': u'try pho chicken (via @foodspotting)', u'user': {u'lastname': u'c.', u'gender': u'male', u'id': u'3893645', u'firstname': u'louis', u'photo': {u'prefix': u'https://irs1.4sqi.net/img/user/', u'suffix': u'/rwfvp1etqt0kwqre.jpg'}}, u'todo': {u'count': 1}, u'id': u'4f396a36e4b0653d4cc83fad', u'logview': true, u'likes': {u'count': 0, u'groups': []}}]
how can sort elements based on timestamp
[createdat key of dict]
?
use list.sort
(inplace sorting) or sorted
(returns new sorted list) , pass key
function. return value of key function used comparison:
lst.sort(key=lambda d: d['createdat'])
Comments
Post a Comment