sorting a list of dictionary values by date in python

后端 未结 5 1280
遥遥无期
遥遥无期 2020-12-24 13:29

I have a list and I am appending a dictionary to it as I loop through my data...and I would like to sort by one of the dictionary keys.

ex:

data = \         


        
5条回答
  •  旧时难觅i
    2020-12-24 13:53

    If you're into the whole brevity thing:

    data = "data from database"
    sorted_data = sorted(
        [{'title': x.title, 'date': x.created_on} for x in data], 
        key=operator.itemgetter('date'),
        reverse=True)
    

提交回复
热议问题